cutout with python

There is a well-finished project on GitHub, rembg

Simple and practical tool to delete image background/cutout
can remove the background of the picture and cut out the main content.

insert image description here

how to use?

1. Installation

CPU support:

pip install rembg

GPU support:

pip install rembg[gpu]

There are a lot of things to install, and you can use it after the installation is complete

2. use

There are many ways to use it; you can type a line of command to cut out the image directly, you can also deploy it to the server and use the browser to cut out the image, you can also write py code to cut out the image, you can also use docker, etc.

2.1 Using the command line

2.1.1. In the command line, switch to the virtual environment used. My virtual environment is named py310

conda activate py310

2.1.2. Start cutout

# 1、扣本地的图
rembg i path/to/input.png path/to/output.png
# 2、扣网上的图
curl -s http://input.png | rembg i > output.png
# 3、扣整个文件夹的图
rembg p path/to/input path/to/output

Before cutout: (picture from Baidu)
Please add a picture description

After cutout:
Please add a picture description

2.2 Use as a service

That is to say, it can be deployed on your own cloud server, and you can cut out pictures anytime, anywhere for the entire webpage or app.

2.2.1 Official web version

# 在虚拟环境中,敲以下命令:
rembg s

insert image description here

The service address will be prompted: http://0.0.0.0:5000
Note that this address is not opened directly, and a docs must be added later, that is: http://0.0.0.0:5000/docs The
get method is applicable to URL addresses on the Internet
The post method is suitable for file streams, here we use the post method
insert image description here

TenTry it out
insert image description here

upload image
insert image description here

point execute
insert image description here

Get the cutout result:
insert image description here

2.2.2 Make your own website

Create a new html file and add the following code:

<form
    action="http://http://0.0.0.0:5000"
    method="post"
    enctype="multipart/form-data"
>
    <input type="file" name="file" />
    <input type="submit" value="upload" />
</form>

open html file
insert image description here

After uploading the file in the browser, the image will be cut out automatically
insert image description here

insert image description here

2.3 Use as a python library

2.3.1 In the form of file stream

Input and output as bytes

from rembg import remove

input_path = 'input.png'
output_path = 'output.png'

with open(input_path, 'rb') as i:
    with open(output_path, 'wb') as o:
        input = i.read()
        output = remove(input)
        o.write(output)

2.3.2 Using PIL images

Input and output as a PIL image

from rembg import remove
from PIL import Image

input_path = 'input.png'
output_path = 'output.png'

input = Image.open(input_path)
output = remove(input)
output.save(output_path)

2.3.3 Even use numpy array

Input and output as a numpy array

from rembg import remove
import cv2

input_path = 'input.png'
output_path = 'output.png'

input = cv2.imread(input_path)
output = remove(input)
cv2.imwrite(output_path, output)

2.4 Use in docker mode (try this later)

docker run -p 5000:5000 danielgatis/rembg s

After writing so much, I ask a soul question:
As a programmer, what is the use of cutout? I usually use it when I make ppt. However, it seems that many softwares have built-in cutout function, so everyone interested in the rembg project can just play around.

For example:
Figure 1 is the appearance of ppt without cutout; Figure 2 is the appearance of ppt after cutout. Figure 2 has less occlusion to the original ppt, and there is no sense of disobedience.
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/xkukeer/article/details/127074784