#!/usr/bin/env python3
from PIL import Image, ImageDraw, ImageOps
# Open PNG image and ensure no alpha channel
im = Image.open('start.png').convert('RGB')
# Draw alpha layer - black square with white circle
alpha = Image.new('L', (100,100), 0)
ImageDraw.Draw(alpha).ellipse((10,10,90,90), fill=255)
# Add our lovely new alpha layer to image
im.putalpha(alpha)
# Save result as PNG and GIF
im.save('result.png')
im.save('unhappy.gif')
当我到达这里时,PNG工作正常,GIF是“不快乐的”
PNG如下:
下面是“不快乐”的GIF:
下面是我如何设置GIF的:
# Extract the alpha channel
alpha = im.split()[3]
# Palettize original image leaving last colour free for transparency index
im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
# Put 255 everywhere in image where we want transparency
im.paste(255, ImageOps.invert(alpha))
im.save('result.gif', transparency=255)
至少对我来说,你该怎么做还不清楚!对于一个不存在的问题,这可能是一个不必要的解决方法,因为我不知道PIL在内部是如何工作的
不管怎样,我用这个输入图像处理它已经够久了:
当我到达这里时,PNG工作正常,GIF是“不快乐的”
PNG如下:
下面是“不快乐”的GIF:
下面是我如何设置GIF的:
关键词:Python、图像处理、PIL、枕头、GIF、透明度、alpha、保留、透明索引
相关问题 更多 >
编程相关推荐