wGrow - Team Notes

Sharing Expertise: Tech Insights and Case Studies

Integrating Python's Rembg Library with C# for Background Removal in .NET Applications

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:

  1. Python 3.7 or later installed
  2. .NET 5 or later installed
  3. 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.

Related

Case Study: Deployment of Bluetooth Components for Airpufying Equipment Management

Case Study: Deployment of Bluetooth Components for Airpufying Equipment Management

Deploy Bluetooth components into the equipment and design a Server-Based System with a Web Interface...

Read More >
SQL Server Hardening Checklist & Best Practices

SQL Server Hardening Checklist & Best Practices

It's essential to ensure that SQL Server is secure and protected against potential cyber threats...

Read More >
Case Study: Setting up a Network Firewall for an Internet Application Server

Case Study: Setting up a Network Firewall for an Internet Application Server

In today's world, securing networks from possible threats and attacks is more than a necessity; ...

Read More >
Securing Data in ASP.NET Web Projects with SQL Server Transparent Data Encryption (TDE)

Securing Data in ASP.NET Web Projects with SQL Server Transparent Data Encryption (TDE)

This technical article provides an overview of SQL Server Transparent Data Encryption (TDE) and demo...

Read More >
TECOM, Order Processing System

TECOM, Order Processing System

TECOM is our in-house ERP system for Order Process. System is great for sellers running multiple sho...

Read More >
Creating a Desktop Application to Backup Gmail Emails and Restore to Gmail or Yahoo Email using C#

Creating a Desktop Application to Backup Gmail Emails and Restore to Gmail or Yahoo Email using C#

Backing up important emails from your Gmail account is essential to ensure data security and availab...

Read More >
Contact Us
  • Our Address:
    114 Lavender Street, #07-51, CT Hub 2, Singapore 338729
    Malaysia Johor - 99-01 Jalan Adda 3/1 Taman Adda Height 81100 Johor Bahru Johor, Malaysia
  • Phone Number:
    +65 6652 3398
  • WhatsApp:
    WhatsApp Us
  • Email:
    [email protected]