程序员是如何制作证件照的

这块我们采用蒙娜丽莎作为示例。


对比一下效果,是不是感觉人工智能特别强大?这块您也可以研究一下他们是怎么做这个背景识别的问题。
移除背景之后,我们就将其下载下来。注意要下载大文件的话需要注册和积分,但一般个人使用基本小图就够了,商业的话您可能需要花点现金了。
下载好我们的蒙娜丽莎,我们要做的就是将这个透明的png图片填充白色的背景(假设证件一般都是白底)。填充背景使用ps来做的话很是简单。但是这块ps破解啥的太麻烦,咋直接使用python来搞。
因为这块python的主要作用就是替换背景色了,所以代码也很简单。
from PIL import Imagedef test():img1 = Image.open("C:\\Users\\Administrator\\Desktop\\meng-removebg-preview.png")img1 = img1.resize((650,788))img1 = img1.convert('RGBA')img1 = transparence2white(img1)img1.show()img1.save("C:\\Users\\Administrator\\Desktop\\meng-removebg-preview-zhengjian.png")def transparence2white(img):sp = img.sizewidth = sp[0]height = sp[1]print(sp)for yh in range(height):for xw in range(width):dot = (xw, yh)color_d = img.getpixel(dot) # 与cv2不同的是,这里需要用getpixel方法来获取维度数据if (color_d[3] == 0):color_d = (255, 255, 255, 255)img.putpixel(dot, color_d) # 赋值的方法是通过putpixelreturn imgif __name__ == '__main__':test()
代码运行完毕之后,我们的桌面就多了一张证件照了,快看看是不是你想要的。

亲,你会了么!
评论
