[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
本文教大家通过Python程序替换证件照背景颜色,以后更换证件照背景就不会再苦恼了。
思路:
1.先去掉原照片的背景颜色
2.再添上新的背景颜色
步骤很简单,思路清晰,操作起来也很简单,十行代码就可以搞定,保证看完你肯定会!
1.去掉原图背景颜色
import os # 去掉背景颜色 os.system('backgroundremover -i "'+str(in_path)+'" -o
"cg_output.jpg"')
in_path是原照片的路径、cg_output.jpg是去掉背景后的照片
2.添加新背景颜色
Python学习交流Q群:906715085### # 加上背景颜色 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是要替换的新颜色,填上对应的英文即可,比如红色: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程序会报错。
样例效果:
左边是原图(蓝色),右边是替换的照片
(蓝换成红)
最后
Python替换证件照背景颜色不难,思路就是先去掉背景,再添上新的背景颜色,相信看到这里的你已经学会了,记得给本文点赞、评论、转发,谢谢~~~