Remove background from multiple images

Did you know that you can remove the background from multiple images located in the specific directory using the pathlib package's module Path and two simple functions from the rembg library? See how:

This notebook was created with MLJAR Studio

MLJAR Studio is Python code editior with interactive code recipes and local AI assistant.
You have code recipes UI displayed at the top of code cells.

Documentation

There are more imports because we also use different recipes.

# import packages
from os import listdir
from os.path import isfile, join
from pathlib import Path
from rembg import remove, new_session
from IPython.display import display, HTML
from PIL import Image

We used List files to show files located in the specific directory:

# list files in directory
my_files = [f for f in listdir("/home/apjanusz/example-images")]
print("Files in directory /home/apjanusz/example-images")
print(my_files)

Here piece of code which allows to remove the background from multiple images in the given directory:

session = new_session()

for file in Path(r"/home/apjanusz/example-images").glob("*.png"):
	input_path = str(file)
	output_path = str(file.parent / (file.stem + ".out.png"))
	with open(input_path, 'rb') as i:
		with open(output_path, 'wb') as o:
			input = i.read()
			output = remove(input, session=session)
			o.write(output)

List of files from the same directory after the operation:

# list files in directory
my_files_after = [f for f in listdir("/home/apjanusz/example-images")]
print("Files in directory /home/apjanusz/example-images")
print(my_files_after)

Below you can find an example of removing the background from one of images located in the directory:

# variables:
path="/home/apjanusz/example-images/zebra.png"
size=500,500

# code:
with Image.open(path) as i:
	i.thumbnail(size)
	display(i)
# variables:
path="/home/apjanusz/example-images/zebra.out.png"
size=500,500

# code:
with Image.open(path) as i:
	i.thumbnail(size)
	display(i)

To display those images we used Show Image.

Below there is a video, which shows how this notebook was created. Enjoy!

HTML("""<iframe width="560" height="315" src="https://www.youtube.com/embed/hPrwQ_3roAw?si=H6PCX7dwwj3vmn2L" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>""")

Conclusions

There are plenty of method that allows you to make your work better or more precise, and this was one of them. Follow MLJAR Studio closely to learn more methods that help you with your work.

See you soon.

# variables:
path="myImage.png"
size=250,250

# code:
with Image.open(path) as i:
	i.thumbnail(size)
	display(i)

Recipes used in the remove-multiple-images-backgrounds.ipynb

All code recipes used in this notebook are listed below. You can click them to check their documentation.

Packages used in the remove-multiple-images-backgrounds.ipynb

List of packages that need to be installed in your Python environment to run this notebook. Please note that MLJAR Studio automatically installs and imports required modules for you.

rembg>=2.0.57

Similar notebooks

List of similar Python notebooks, so you can find more inspiration ๐Ÿ˜Š