How to Remove Image Background Using Python

Original image on the left and background removed image on the right with Python logo in the center, demonstrating image background removal using Python.

Removing the background from an image can be really useful, especially when you want to isolate an object or a person from the image. In this article, weā€™ll explore how to remove background from an image using Python. Weā€™ll use two popular libraries: rembg and Pillow. Both libraries make the process of removing backgrounds simple and fast. Letā€™s dive in!

Visit Also: Learn Image Cropping in Python: 2 Powerful Techniques

Requirements

Before we begin, make sure you have Python installed on your machine. Youā€™ll also need to install the following libraries:

  • rembg ā€“ For removing the background.
  • Pillow ā€“ For handling image manipulation.

You can install both libraries using pip:

pip install rembg Pillow

Original Image

In every example, we will use this image.

a beautiful girl is standing on an empty rail track, holding a flower in her hands, looking straight to the camera.

Example 1: Removing the Background from a Simple Image

Letā€™s start with the basics. In this example, weā€™ll load an image, remove its background using ā€˜rembgā€˜, and save the output as a new image.

from rembg import remove
from PIL import Image

# Open the image file
with open('input_image.png', 'rb') as img_file:
    img_data = img_file.read()

# Remove the background
output_data = remove(img_data)

# Save the result
with open('output_image.png', 'wb') as out_file:
    out_file.write(output_data)

We load the image from a file, remove the background with the ā€˜removeā€˜ function from ā€˜rembgā€˜, and save the output image.

Output

Beautiful girl holding a flower in her hands against a plain white background.

Example 2: Converting Image Format After Removing Background

In this example, weā€™ll remove the background and also convert the image to JPEG format.

from rembg import remove
from PIL import Image
import io

# Open the image file
with open('input_image.png', 'rb') as img_file:
    img_data = img_file.read()

# Remove the background
output_data = remove(img_data)

# Convert the output to an image
output_image = Image.open(io.BytesIO(output_data))

# Convert and save as JPEG
output_image.convert('RGB').save('output_image.jpg', 'JPEG')

In the above example, we use Pillow to convert the background-removed image into JPEG format since PNG supports transparency, but JPEG does not.

Output

Beautiful girl holding a flower in her hands against a plain black background.

Example 3: Resizing the Image After Background Removal

Sometimes, after removing the background, you may want to resize the image. Hereā€™s how you can do that.

from rembg import remove
from PIL import Image
import io

# Open the image file
with open('input_image.png', 'rb') as img_file:
    img_data = img_file.read()

# Remove the background
output_data = remove(img_data)

# Load the output image
output_image = Image.open(io.BytesIO(output_data))

# Resize the image
resized_image = output_image.resize((300, 300))

# Save the resized image
resized_image.save('resized_output_image.png')

After removing the background, we use ā€˜Pillowā€˜ to resize the image to 300Ɨ300 pixels and save it.

Output

Beautiful girl holding a flower in her hands, transparent background

Example 4: Adding a New Background to the Image

In this final example, weā€™ll remove the background and add a new solid background color to the image.

from rembg import remove
from PIL import Image, ImageOps
import io

# Open the image file
with open('input_image.png', 'rb') as img_file:
    img_data = img_file.read()

# Remove the background
output_data = remove(img_data)

# Load the output image
output_image = Image.open(io.BytesIO(output_data))

# Create a solid background (white)
new_background = Image.new('RGB', output_image.size, (255, 255, 255))

# Paste the image without the background on top of the white background
new_background.paste(output_image, mask=output_image.split()[3])

# Save the final image
new_background.save('new_background_image.png')

We remove the background and create a new white background using Pillow. Then we paste the object from the original image onto the new background and save it.

Output

Beautiful girl holding a flower in her hands, green background

Visit Also: How to Compress an Image using Python: A Detailed Guide

Summary

In this tutorial, we learned how to remove the background from an image using Pythonā€™s rembg and Pillow libraries. We covered four programming examples here performing background removing, changing the format, resizing, and adding a new background.

Please feel free to experiment with your own images, and youā€™ll find it easy to customize these examples to fit your needs.

Happy coding!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! Iā€™m Subhankar Rakshit, the brains behind PySeek. Iā€™m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles:Ā 194