
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:
You can install both libraries using pip:
pip install rembg Pillow
Original Image
In every example, we will use this image.

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

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

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

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

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!