使用ruby调整动画GIF图像的大小? [英] Resizing Animated GIF images with ruby?

查看:189
本文介绍了使用ruby调整动画GIF图像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将GIF图像调整为不同尺寸。我在ruby中使用RMagick库。但是对于一些gif图像来说,即使我缩小GIF,文件大小也会增加。我正在以相同的宽高比调整图像图像的大小。这是我的代码。

I am trying to resize a GIF image to different sizes. I am using RMagick library in ruby. But it seems for some gif images the file size increases even when I am downsizing GIF. And I am resizing the image image in same aspect ratio. Here is the my code.

require 'rmagick'
path = "/path/to/file/"
s_image = "image.gif" # image is 320*320
t_image = "t.gif"
file_name = path+s_image
file = File.new(file_name)
list = Magick::ImageList.new.from_blob file.read
list = list.coalesce.remap
list.each do |x|
  x.resize_to_fill!(256,256)
end
File.open("#{path+t_image}", 'wb') { |f| f.write list.to_blob }

我缺少什么?

推荐答案

您链接的图片由35帧组成。它也已经过优化,因此在第一帧之后,每帧只包含不同的像素。大多数像素都是透明的,因为移动很少。这是GIF动画的常见情况 - 如果没有太多的变化或相机移动,它们可以相对有效(就文件大小而言)。

The image you linked consisted of 35 frames. It had also been optimised, so that after than the first frame, each frame only contains the pixels that are different. Most of the pixels are transparent, because very little moves. This is common situation with animated gifs - they can be made relatively efficient (in terms of file size) if there are not too many changes or camera movement.

此外,每个帧大小是包含所有变化像素所需的最小矩形,因此它在帧与帧之间变化。

In addition, each frame size is the minimum rectangle necessary to contain all the changing pixels, so it varies from frame-to-frame.

如果您将原始图像加载到例如: GIMP,并检查各个图层。

You can see this clearly, if you load the original image in e.g. The GIMP, and inspect the individual layers.

如果您对转换后的图像执行此操作,还可以看到您的代码完整地渲染每个帧,以便重新 - 准确的大小。作为其副作用,它会增加文件大小。将图像x,y缩小到一半应该意味着输出文件大小约为原始文件的1/4。但是,将每个帧从几个像素差异转换为全帧会使该大小倍增。由于有35个帧,这可以弥补较小的宽度和高度。

If you do that with your converted image, can also see that your code renders out each frame in full, in order to re-size accurately. As a side effect of this, it increases the file size. Reducing the image x,y to half should mean your output file size is roughly 1/4 of the original. However, turning each frame from just a few pixels difference to the full frame multiplies the size dramatically. As there are 35 frames, this more than makes up for the smaller width and height.

幸运的是,ImageMagick(和Ruby中的Rmagick绑定)包含一个重新优化的功能GIF返回图层,只有差异存储为可见像素。您需要添加对此 optimize_layers 方法的调用,以允许您的代码具有较小的文件大小。此外,为了获得最佳文件大小,您需要停止使用 .remap ,这会改变像素值,使优化器不起作用。

Luckily, ImageMagick (and Rmagick bindings in Ruby) includes a function for re-optimising the GIF back to layers with only the differences stored as visible pixels. You need to add a call to this optimize_layers method to allow your code to have a lower file size. In addition, for best file size you need to stop using .remap which is altering pixel values enough that the optimiser does not work.

require 'rmagick'
path = "/path/to/file/"
s_image = "s_image.gif" # image is 320*320
t_image = "t_image.gif"
file_name = path+s_image
file = File.new(file_name)
list = Magick::ImageList.new.from_blob file.read

# This renders out each GIF frame in full, prior to re-sizing
# Note I have removed the .remap because it alters pixel values
# between frames, making it hard to optimise
list = list.coalesce

list.each do |x|
  x.resize_to_fill!(256,256)
end

# Re-optimize the GIF frames
list = list.optimize_layers( Magick::OptimizeLayer )

File.open("#{path+t_image}", 'wb') { |f| f.write list.to_blob }

这篇关于使用ruby调整动画GIF图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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