Home  >  Article  >  Backend Development  >  Ten lines of Python code to replace the background color of ID photos

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

WBOY
WBOYforward
2023-05-03 14:01:062072browse

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

This article teaches you how to change the background color of your ID photo through a Python program, so that you won’t have to worry about changing the background of your ID photo in the future.

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

Idea:

  • First remove the background color of the original photo
  • and then add a new background Color

The steps are very simple, the idea is clear, and the operation is also very simple. It can be done in ten lines of code. I guarantee you will know it after reading it!

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

1. Remove the background color of the original image

import os
# 去掉背景颜色
os.system('backgroundremover -i "'+str(in_path)+'"-o "cg_output.jpg"')

in_path is the path of the original photo, cg_output.jpg is the one after removing the background Photo

Tip: For the specific use of the backgroundremover library, please refer to my previous article (one line of Python code to remove photo background)

2. Add a new 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, just fill in the corresponding English, such as red: red

color = "red"
# 红:red、蓝:blue、黑:black、白:white

Full code

import os
from PIL import Image
# 输入
in_path = "replace.jpg"
# 输出
out_path = "out.png"
# 要替换的背景颜色
color = "red"
# 红:red、蓝:blue、黑:black、白:white
# 去掉背景颜色
os.system('backgroundremover -i "'+str(in_path)+'"-o "cg_output.jpg"')
# 加上背景颜色
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)

General steps of the code:

Replace replace.jpg (photo with blue background) with photo out.png with red (color) background color

Here, readers are reminded that the output photo (out.png) must be saved in png format, and the program will report an error in other formats such as jpg.

Sample effect:

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

(Picture source network)

The left side is the original picture (blue), and the right side is the replaced photo

(Blue to red)

Summary

It is not difficult to change the background color of the ID photo with Python. The idea is to remove the background first and then add a new one. Background color, I believe you have learned it after seeing this.

The above is the detailed content of Ten lines of Python code to replace the background color of ID photos. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete