python png白色背景转透明背景

个人博客,已转移至http://www.zgeo.work:8022/wordpress

python png白色背景转透明背景,复制文件后可直接运行

# -*- coding: utf-8 -*-
import os
from PIL import Image

def transparent_back(filename):
    if(filename.find("png")>=0):
        img = Image.open(filename)
        L, H = img.size
        color_0 = (255,255,255,255)#要替换的颜色
        is_saved=False
        for h in range(H):
            for l in range(L):
                dot = (l,h)
                color_1 = img.getpixel(dot)
                if color_1 == color_0:
                    is_saved=True
                    color_1 = color_1[:-1] + (0,)
                    img.putpixel(dot,color_1)
        if is_saved :
            img.save(filename, "PNG")
               
def get_files_name(current_dir):
    for file_list in os.walk(current_dir):
         filesubdir=file_list[0]
         filenames=file_list[2]
         print("current_dir--"+ file_list[0])
         if(len(filenames)>0):
            for i in filenames:
                transparent_back(filesubdir+"\\"+i)

get_files_name(png_dir)

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值