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

yipeiwu_com6年前Python基础

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

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)

以上这篇python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Queue 实现生产者消费者模型(实例讲解)

Python中,队列是线程间最常用的交换数据的形式。 Python Queue模块有三种队列及构造函数: 1、Python Queue模块的FIFO队列先进先出。 class Queue...

Python解析最简单的验证码

Python解析最简单的验证码

最近在学python,正好遇到学校需要选宿舍,就用python写了一个抢宿舍的软件。其中有一个模块是用来登陆的,登陆的时候需要输入验证码,不过后来发现了直接可以绕过验证码直接登陆的bug...

python解决方案:WindowsError: [Error 2]

使用Python的rename()函数重命名文件时出现问题,提示 WindowsError: [Error 2] 错误,最初代码如下: def renameFile(filename...

python文字和unicode/ascll相互转换函数及简单加密解密实现代码

这篇文章主要介绍了python文字和unicode/ascll相互转换函数及简单加密解密实现代码,下面我们来了解一下。 import re import random # ord()...

python:socket传输大文件示例

文件可以传输,但是对比传输前后的文件:socket_test.txt,末尾有一些不一致服务端代码: #!/usr/bin/python # -*- coding: utf-8 -*-...