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

Optimizing a Large-Scale Medical Service Provider's Patient Management System Using Cost-Effective Scalability Solutions

Optimizing a Large-Scale Medical Service Provider's Patient Management System Using Cost-Effective Scalability Solutions

This article outlines the process of improving the performance of a commercial medical service provi...

Read More >
Case Study: Virtualizing a Legacy Windows Server 2000 Application for Improved Security and Maintainability

Case Study: Virtualizing a Legacy Windows Server 2000 Application for Improved Security and Maintainability

In this case study, we describe the process of virtualizing a legacy Windows Server 2000 application...

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 >
Implementing Business Continuity Management for a Singapore Real Estate Agency's ERP Solution

Implementing Business Continuity Management for a Singapore Real Estate Agency's ERP Solution

This technical summary documents the process of designing and implementing a Business Continuity Man...

Read More >
Leveraging Excel VBA and Forms for Rapid Development of a Time-Sensitive Sales Order Management System

Leveraging Excel VBA and Forms for Rapid Development of a Time-Sensitive Sales Order Management System

In the world of enterprise software development, it is crucial to balance the need for rapid deploym...

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]