程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Ten lines of Python code to replace the background color of ID photo

編輯:Python

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 :

summary

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

About Python Technology reserve

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 !

One 、Python Learning routes in all directions

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 .

Two 、 Learning software

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 .

3、 ... and 、 Getting started video

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 .

Four 、 Practical cases

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 .

5、 ... and 、 Interview information

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 .


This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved