This project demonstrates how to use the rembg
library in combination with Pillow
to remove the background from an image. It reads an input image, removes its background, and saves the result.
Make sure you have Python installed on your system. You'll also need to install the following Python library:
You can install these libraries using pip
:
pip install rembg
-
Clone the repository or download the script.
git clone https://github.com/ahmed-sitl/remove-background.git cd remove-background
-
Place the image you want to process in the same directory as the script and update the
input_path
variable inmain.py
to the image file name. -
Run the script:
python main.py
-
The output image with the background removed will be saved in the same directory as
ahmed.jpg
.
from rembg import remove
from PIL import Image
# Input path
input_path = 'ahmed.jpeg'
# Output path
output_path = 'ahmed.jpg'
# Open input image
input = Image.open(input_path)
# Remove background
output = remove(input)
# Convert image to RGB if it's in RGBA mode
if output.mode == 'RGBA':
output = output.convert('RGB')
# Save the output image
output.save(output_path)
Here is an example of the input image and the output image after background removal:
This project is licensed under the MIT License - see the LICENSE file for details.