relustarry
# -*- 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)

 

分类:

技术点:

相关文章:

  • 2021-12-03
  • 2021-05-10
  • 2021-05-24
  • 2021-11-18
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2021-04-09
  • 2022-01-17
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-09-13
相关资源
相似解决方案