Introduction:
In this article, we will explore how to integrate Python's Rembg library with C# to add background removal functionality to a .NET application. Rembg is an open-source library available on GitHub that utilizes deep learning models to effectively remove the background of images. Although the library is written in Python, we can still leverage it in our C# application using the Python.NET package. This will allow us to call Python functions and use Python objects directly from our C# code.
Note: we implement the related functions in a system as a module, but we copy the key codes out to illustrate the feature.
Prerequisites:
- Python 3.7 or later installed
- .NET 5 or later installed
- Visual Studio or Visual Studio Code
Step 1: Install Python.NET and Rembg
First, we need to install the Python.NET package and the Rembg library. To do this, open a terminal or command prompt and run the following commands:
pip install pythonnet
pip install rembg
Step 2: Create a .NET Console Application
Next, we'll create a new .NET Console Application. Open Visual Studio, create a new project, and select the "Console App" template. Name the project "BackgroundRemovalApp".
Step 3: Add Python.NET to the Project
To add Python.NET to the project, open the Package Manager Console in Visual Studio and run the following command:
Install-Package Python.Runtime.NETCore -Version 3.7.0
Step 4: Import Python and Rembg Libraries in C#
Now, we'll import the necessary Python and Rembg libraries in our C# code. In the "Program.cs" file, add the following using statements:
using System.IO;
using Python.Runtime;
Step 5: Implement the Background Removal Function
Let's now create a function that uses the Rembg library to remove the background from an image. This function will accept a file path as input and output the processed image to a specified directory. Add the following code to the "Program.cs" file:
public static Bitmap NumpyArrayToBitmap(dynamic npArray)
{
var npArrayBak = npArray;
int height = npArray.shape[0];
int width = npArray.shape[1];
int channels = npArray.shape[2];
if (channels != 3 && channels != 4)
{
throw new ArgumentException("Invalid number of channels in the NumPy array.");
}
var img = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var rect = new Rectangle(0, 0, width, height);
BitmapData bmpData = img.LockBits(rect, ImageLockMode.WriteOnly, img.PixelFormat);
IntPtr ptr = bmpData.Scan0;
byte[] bytes = new byte[width * height * 4];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int position = (y * width + x) * 4;
int positionNp = (y * width + x) * channels;
bytes[position] = (byte)npArray[y][x][2]; // Blue
bytes[position + 1] = (byte)npArray[y][x][1]; // Green
bytes[position + 2] = (byte)npArray[y][x][0]; // Red
bytes[position + 3] = channels == 4 ? (byte)npArray[y][x][3] : (byte)255; // Alpha
}
}
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, bytes.Length);
img.UnlockBits(bmpData);
return img;
}
public static void RemoveBackground(string inputImagePath, string outputImagePath)
{
using (Py.GIL())
{
dynamic rembg = Py.Import("rembg");
dynamic np = Py.Import("numpy");
dynamic cv2 = Py.Import("cv2");
var npArray = cv2.imdecode(np.fromfile(inputImagePath, np.uint8), -1);
var outputData = rembg.remove(npArray);
Bitmap resultBitmap = NumpyArrayToBitmap(outputData);
resultBitmap.Save(outputImagePath, ImageFormat.Png);
}
}
Step 6: Test the Background Removal Function
Finally, let's test our background removal function. In the "Main" method of the "Program.cs" file, add the following code to remove the background from a sample image:
static void Main(string[] args)
{
// Initialize the Python runtime
PythonEngine.Initialize();
// Define input and output file paths
string inputImagePath = "sample_image.jpg";
string outputImagePath = "output_image.png";
// Call the RemoveBackground function
RemoveBackground(inputImagePath, outputImagePath);
Console.WriteLine("Background removal completed!");
}
Conclusion:
In this article, we demonstrated how to integrate Python's Rembg library with C# to add background removal functionality to a .NET application. By using the Python.NET package, we were able to call Python functions and use Python objects directly from our C# code. This approach can be extended to other Python libraries, allowing .NET developers to leverage the extensive ecosystem of Python packages in their applications.