我想知道是否可以使用PIL缩放动画GIF图像。特别是Plone的原型的ImageField目前失去了图像上的动画使用其规模缩小方法:如何在PIL中缩放动画GIF图像并保留动画
def scale(self, data, w, h, default_format = 'PNG'):
""" scale image (with material from ImageTag_Hotfix)"""
#make sure we have valid int's
size = int(w), int(h)
original_file=StringIO(data)
image = PIL.Image.open(original_file)
# consider image mode when scaling
# source images can be mode '1','L,','P','RGB(A)'
# convert to greyscale or RGBA before scaling
# preserve palletted mode (but not pallette)
# for palletted-only image formats, e.g. GIF
# PNG compression is OK for RGBA thumbnails
original_mode = image.mode
img_format = image.format and image.format or default_format
if original_mode == '1':
image = image.convert('L')
elif original_mode == 'P':
image = image.convert('RGBA')
image.thumbnail(size, self.pil_resize_algo)
# decided to only preserve palletted mode
# for GIF, could also use image.format in ('GIF','PNG')
if original_mode == 'P' and img_format == 'GIF':
image = image.convert('P')
thumbnail_file = StringIO()
# quality parameter doesn't affect lossless formats
image.save(thumbnail_file, img_format, quality=self.pil_quality)
thumbnail_file.seek(0)
return thumbnail_file, img_format.lower()
我知道如何识别一个GIF动画:下面的计算结果为true image.format == 'GIF' and image.seek(image.tell()+1)
。我试过不转换到RGBA模式,但不起作用。
背景:在我们的Plone实例中,我们修改了默认图像类型以设置其图像字段的original_size属性,以强制所有图像使用适当的质量设置进行缩放。这对jpegs非常适用,但意味着我们目前无法上传动画GIF
缩放调色板图像可能会导致图像质量不可接受。 – 2012-02-03 15:09:29