欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用Python替換證件照背景顏色

 更新時間:2022年01月25日 14:27:41   作者:晨xi的光  
大家好,本篇文章主要講的是用Python替換證件照背景顏色,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

前言

本文教大家通過Python程序替換證件照背景顏色,以后更換證件照背景就不會再苦惱了。

思路

先去掉原照片的背景顏色

再添上新的背景顏色

步驟很簡單,思路清晰,操作起來也很簡單,十行代碼就可以搞定,保證看完你肯定會!

1.去掉原圖背景顏色

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

in_path是原照片的路徑、cg_output.jpg是去掉背景后的照片

2.添加新背景顏色

# 加上背景顏色
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是替換背景顏色后的照片路徑,color是要替換的新顏色,填上對應(yīng)的英文即可,比如紅色:red

color = "red"
# 紅:red、藍:blue、黑:black、白:white

完整代碼

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)

代碼大體步驟:

將replace.jpg(藍色背景的照片),換成紅色(color)的背景顏色的照片out.png

這里提醒一下讀者,輸出的照片(out.png)要保存成png的格式,其他格式如jpg程序會報錯。

樣例效果:

(圖片來源網(wǎng)絡(luò))

左邊是原圖(藍色),右邊是替換的照片

總結(jié)

到此這篇關(guān)于用Python替換證件照背景顏色的文章就介紹到這了,更多相關(guān)Python證件照背景顏色內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論