如何在PIL中缩放动画GIF图像并保留动画 [英] How do you scale an animated GIF image in PIL and preserve the animation

查看:512
本文介绍了如何在PIL中缩放动画GIF图像并保留动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用PIL缩放动画GIF图像。特别是Plone的原型ImageField目前从使用其缩放方法缩放的图像中丢失动画:

I'm wondering if it's possible to scale an animated GIF image using PIL. In particular Plone’s archetypes ImageField currently loses the animation from images scaled using its scale method:

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'和image.seek(image.tell()+ 1)。我尝试过不转换为RGBA模式,但这不起作用。

I know how to identify a animated GIF: The following evaluates to True image.format == 'GIF' and image.seek(image.tell()+1). I’ve tried not converting to RGBA mode but that doesn't work.

背景:在我们的Plone实例上,我们修改了默认图像类型以设置original_size属性其图像区域强制所有图像使用适当的质量设置进行缩放。这适用于jpeg,但意味着我们目前无法上传动画GIF

Background: On our Plone instance we've modified the default image type to set the original_size attribute of its image field to force all images to scaled with an appropriate quality setting. This works great for jpegs but means we currently can't upload animated GIFs

推荐答案

PIL对动画GIF的支持有限但是如上所说,它是有限的,你必须在非常低的水平上工作来处理它。

PIL has got some limited support for animated GIF's but it is, as said, limited, and you have to work in very low level to deal with it.

如果你想处理GIF动画,我建议尝试使用其他方法缩放图像而不是PIL。可能,最简单的方法是使用offprocess.Popen进行进程外ImageMagick - (即便在那时,我只是猜测ImageMagick使用动画GIF做正确的事情) -

I'd advise trying some other method for scaling images than PIL if you want to deal with animated gif's. Possibly, the most straightforward way is to use an off-process ImageMagick, with subprocess.Popen - (and even then, at this time, I am only guessing ImageMagick "does the right thing" with animated GIF's) -

一个选项是拥有一个图像处理服务器,使用另一个Python脚本,除了你的zope安装,它将接收扩展图像的请求 - 可能是通过xmlrpc调用 - 你可以构建这是一个GIMP插件,使用GIMP缩放GIF。

An option is to have an "image processing server", with another Python script, apart from your zope install that will receive requests to scale the images - possibly with a xmlrpc call - you could build this as a GIMP plug-in and use GIMP for scaling the GIF's.

另一个选择是保持原样,并使用剧照动画GIF的地方需要出现在与原始尺寸不同的另一个尺寸中,并在动画适当的位置显示原始图像。 (或者可能只是要求动画gif已经以适当的大小提交)

Another option is to leave things as they are, and use "stills" for animated GIF's where they need to appear in another dimension than the original, and display the original image where the animation is appropriate. (Or maybe simply require that animated gif's be submitted already on the proper size)

这篇关于如何在PIL中缩放动画GIF图像并保留动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆