How To Remove Backgrounds From Images With Python
python It is a multi-purpose programming language.Create, do a lot web app Check who is participating international space station with Raspberry Pi Pico W.
Python is easy to read and easy to learn. What makes Python so versatile are pre-written modules of code, sometimes called “libraries”. These modules bring new functionality. For example, RPi.GPIO allows Python to control a module’s GPIO. Raspberry Pi.
This how-to uses two Python modules to create a GUI application that removes the background from an image. first module, Rembu from Daniel Gatiss Remove the background from the presented image. The second module, easygui, provides a way to create dialogs and menus using the operating system’s toolkit. Therefore, the file open/save dialog boxes look exactly like those used in many other applications.
To take this project further, you can create your application in the following ways: Package your project code into a single executable.
set up
Before we can write a line of code, we need to sort everything out. First, create a folder to store the images you will be working with. Next, open a Python editor for the coding portion of the project.
1. Create a new folder on your desktop named rembg.
2. In the folder, place the images whose background you want to remove.
3. Open your favorite Python editorwe prefer Tony This is to provide a simple user interface.follow This guide Install Thonny.
Installing Python modules
To use Rembg, you first need to download and install the Python module. This can be handled via Thonny’s built-in package manager, or Python’s packaging tool pip.
Install via Thonny
1. Click Tools >> Manage Packages.
2. Search for rembg and[PyPi で検索]Click. Select rembg from the returned list.
3. [インストール]to download and install rembg for Python. The system already has rembg installed, so there is an uninstall button.
Four. Repeat steps 2 and 3, but this time search for easygui and install it.
Installation via pip
If you’re using another Python editor, you’ll need to use pip to install the Python package.
1. Open a command prompt and install rembg. Press Enter to start the process.
pip install rembg
2. Install easygui using pip. Easygui provides a basic user interface for opening and saving files.
pip install easygui
write code
The code is very simple in nature, just 8 lines of Python to remove the background from any image. The underlying modules rembg and easygui do all the heavy lifting for you.
1. Import the remove class from the rembg module. This is what I use to remove the background.
from rembg import remove
2. Import an Image from the Python Imaging Library (PIL). PIL is a powerful module containing various options for creating and manipulating images and image streams.
from PIL import Image
3. Import the easygui module and create a reference as ‘eg’. Easygui is a GUI for basic file operations. Renaming it to ‘eg’ makes things easier.
import easygui as eg
Four. Create an object input_path and use it to store the path and name of the file you want to remove the background from. Use easygui’s open file dialog box and give the dialog a title to describe its purpose. The selected file and its path are stored in the input_path object.
input_path = eg.fileopenbox(title="Select image file")
Five. Create an object output_path and use easygui’s save file dialog to capture the file path and save it to the object.
output_path = eg.filesavebox(title="Save file to..")
6. Create an object, populate it, use it, open and save the image via PIL’s Image.open . function.
input = Image.open(input_path)
7. Use rembg to remove the background from the image.
output = remove(input)
8. Save the new image using the file path stored in output_path.
output.save(output_path)
9. Save the code as background_remover.py.
Ten. [実行]Click the button to run the code.
11. Select the image you want to remove the background from. Notice that the dialog has the title that we specified in the code.
12. Go to the rembg folder, name the file, and set the file format to PNG.[保存]Click. For this example, save the file as les-no-bg.png.
13. There may be errors, but this is expected. Download the u2net-file Save it in the .u2net folder in your user directory. This folder is automatically created and saved.
14. Go back to step 10 and rerun your code. No errors this time, so skip step 13.
15. Go to the rembg folder and you will be able to use the image.
complete code listing
from rembg import remove
from PIL import Image
import easygui as eg
input_path = eg.fileopenbox(title="Select image file")
output_path = eg.filesavebox(title="Save file to..")
input = Image.open(input_path)
output = remove(input)
output.save(output_path)