网络编程
位置:首页>> 网络编程>> Python编程>> python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例

python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例

作者:拓宽视野  发布时间:2021-04-01 05:54:41 

标签:python3,4通道,1通道,3通道

我就废话不多说了,直接上代码吧!


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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com