我想知道是否可以使用PIL缩放动画GIF图像。特别是Plone的原型ImageField目前在使用其缩放方法缩放图像时会丢失动画:
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属性,以强制所有图像使用适当的质量设置进行缩放。这对于JPEG图像非常有效,但意味着我们目前无法上传动画GIF。