美文网首页
change images to RGB

change images to RGB

作者: 狼无雨雪 | 来源:发表于2019-12-18 18:33 被阅读0次
    import os
    from PIL import Image
    import sys
    def change_image_channels(input_image_path):
        image = Image.open(input_image_path)
        if image.mode == 'RGBA':
            r, g, b, a = image.split()
            image = Image.merge("RGB", (r, g, b))
            try:
                os.remove(input_image_path)
            except:
                pass
            image.save(input_image_path)
        elif image.mode != 'RGB':
            image = image.convert("RGB")
            try:
                os.remove(input_image_path)
            except:
                pass
            image.save(input_image_path)
        else:
            pass
        return image
    
    if __name__ == "__main__":
        input_dir = sys.argv[1]
        for root, dirs, files in os.walk(input_dir):
            for file in files:
                path = os.path.join(root, file)
                change_image_channels(path)
    

    相关文章

      网友评论

          本文标题:change images to RGB

          本文链接:https://www.haomeiwen.com/subject/algunctx.html