您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用PIL从任何图像中删除透明度/ alpha

5b51 2022/1/14 8:22:31 python 字数 1139 阅读 526 来源 www.jb51.cc/python

如何用指定的背景颜色替换任何图像(png,jpg,rgb,rbga)的alpha通道?它还必须适用于没有Alpha通道的图像. 这可以通过检查图像是否透明来完成 def remove_transparency(im, bg_colour=(255, 255, 255)): # Only process if image has transparency (https://stackove

概述

def remove_transparency(im,bg_colour=(255,255,255)):

    # Only process if image has transparency (https://stackoverflow.com/a/1963146)
    if im.mode in ('RGBA','LA') or (im.mode == 'P' and 'transparency' in im.info):

        # Need to convert to RGBA if LA format due to a bug in PIL (https://stackoverflow.com/a/1963146)
        alpha = im.convert('RGBA').split()[-1]

        # Create a new background image of our matt color.
        # Must be RGBA because paste requires both images have the same format
        # (https://stackoverflow.com/a/8720632  and  https://stackoverflow.com/a/9459208)
        bg = Image.new("RGBA",im.size,bg_colour + (255,))
        bg.paste(im,mask=alpha)
        return bg

    else:
        return im

总结

以上是编程之家为你收集整理的使用PIL从任何图像中删除透明度/ alpha全部内容,希望文章能够帮你解决使用PIL从任何图像中删除透明度/ alpha所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶