python
主页 > 脚本 > python >

python3 图片4通道转成3通道 1通道转成3通道图片压缩教程

2019-12-03 | 秩名 | 点击:
代码如下:

from PIL import Image
# 通道转换
def change_image_channels(image, image_path):
    # 4通道转3通道
  if image.mode == 'RGBA':
        r, g, b, a = image.split()
        image = Image.merge("RGB", (r, g, b))
        image.save(image_path)
    # 1 通道转3通道
    elif image.mode != 'RGB':
        image = image.convert("RGB")
        os.remove(image_path)
        image.save(image_path)
    return image
  
# 图片压缩
def image_compression(image):
   w, h = image.size
   print(w, h)
   image.thumbnail((int(w / 1.1), int(h / 1.1)))
   image.save("./car.png")
  return image
  
if __name__ == "__main__":
    image = Image.open("./timg.png")
    new_image = process_image_channels(image, "./time.png")
    print(new_image.mode)

原文链接:https://blog.csdn.net/Areigninhell/article/details/86505961
相关文章
最新更新