时间:2020-12-08 23:24:33 | 栏目: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)