This article teaches you to pass Python The program replaces the background color of the certificate photo , In the future, the background of the certificate photo will not be distressed any more .
Ideas :
1. First remove the background color of the original photo
2. Add a new background color
The steps are simple , have a lucid brain , It's easy to operate , Ten lines of code will do it , Make sure you will !
1. Remove the original background color
import os
# Remove the background color
os.system('backgroundremover -i "'+str(in_path)+'" -o "cg_output.jpg"')
in_path Is the path of the original photo 、cg_output.jpg It's a picture with the background removed
2. Add new background color
# Add the background color
no_bg_image = Image.open("cg_output.jpg")
x, y = no_bg_image.size
new_image = Image.new('RGBA', no_bg_image.size, color=color)
new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)
new_image.save(out_path)
out_path Is the photo path after replacing the background color ,color Is the new color to be replaced , Fill in the corresponding English , such as Red :red
color = "red"
# red :red、 blue :blue、 black :black、 white :white
Complete code
import os
from PIL import Image
# Input
in_path = "replace.jpg"
# Output
out_path = "out.png"
# Background color to replace
color = "red"
# red :red、 blue :blue、 black :black、 white :white
# Remove the background color
os.system('backgroundremover -i "'+str(in_path)+'" -o "cg_output.jpg"')
# Add the background color
no_bg_image = Image.open("cg_output.jpg")
x, y = no_bg_image.size
new_image = Image.new('RGBA', no_bg_image.size, color=color)
new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)
new_image.save(out_path)
The general steps of the code :
take replace.jpg( A picture with a blue background ), Change to red (color) The background color of the photo out.png
Here's a reminder to readers , Output photos (out.png) want Save as png The format of , Other formats such as jpg The program will report an error .
Sample effect :
Python Replace the background color of ID photo It's not hard to , The idea is to remove the background first , Add a new background color , I believe that you have learned
Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !
Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .
If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .
When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .
Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .
We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .
Guarantee 100% free
】