编程语言
首页 > 编程语言> > Python将图片背景透明化

Python将图片背景透明化

作者:互联网

def transparent_background(path):
    try:
        img = Image.open(path)
        img = img.convert("RGBA")  # 转换获取信息
        pixdata = img.load()
        color_no = get_convert_middle(path) + 30  # 抠图的容错值

        for y in range(img.size[1]):
            for x in range(img.size[0]):
                if pixdata[x, y][0] > color_no and pixdata[x, y][1] > color_no and pixdata[x, y][2] > color_no and \
                        pixdata[x, y][3] > color_no:
                    pixdata[x, y] = (255, 255, 255, 0)

        if not path.endswith('png'):
            os.remove(path)
            replace_path_list = path.split('.')
            replace_path_list = replace_path_list[:-1]
            path = '.'.join(replace_path_list) + '.png'

        img.save(path)
        img.close()
    except Exception as e:
        return Falsereturn path


def get_convert_middle(img_path):
    I = Image.open(img_path)
    L = I.convert('L')
    im = numpy.array(L)
    im4 = 255.0 * (im / 255.0) ** 2  # 对图像的像素值求平方后得到的图像
    middle = (int(im4.min()) + int(im4.max())) / 2
    return middle

# 调用 transparent_background, 传入图片路径, 该方法把图片修改后替换了源文件

 

标签:pixdata,img,no,Python,replace,color,path,透明化,图片
来源: https://www.cnblogs.com/lab-zj/p/12896305.html