批量处理图片转为透明色
# -*- coding:utf-8 -*- """ __project_ = 'ToBase64' __file_name__ = 'PicToBack' __author__ = 'xbxia' __time__ = '2020/10/28 8:29' __product_name = PyCharm """ import os from PIL import Image def transparent_back(pict_path): # 循环遍历remobg文件夹中的所有图片 for pict in os.listdir(pict_path): img_file = os.path.join(pict_path, pict) #print(img_file) # 执行去除背景色操作 img = Image.open(img_file) img = img.convert('RGBA') #img.show() pixdata = img.load() for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][0] > 220 and pixdata[x, y][1] > 220 and pixdata[x, y][2] > 220 and pixdata[x, y][3] > 220: pixdata[x, y] = (255, 255, 255, 0) # 重命名 name = img_file.split('.') #os.rename(os.path.join(pict_path, name[0] + ".png") img.save(name[0] + ".png") print("%s is done" % img_file) if __name__ == '__main__': pict_path = r"F:\Data\NewDesktop\1" transparent_back(pict_path)
本文来自博客园,作者:ReluStarry,转载请注明原文链接:https://www.cnblogs.com/relustarry/p/13897274.html