Resizing Animated Gif's with RMagick
Dec 1, 2006 @ 4:04 pmI needed to resize some animated gif’s using RMagick, and the solutions I found on the web didn’t work for me, so I thought I would post what I did. The best solution I found was here using FileColumn. Unfortunately it didn’t work out for me. That particular solution assumed that all frames were the same dimension, and that isn’t the case for me. I found that if I scaled down all the frames, the image would come out looking right.
Here is what I came up with:
File.open(ARGV[0], 'rb') { |file|
imgs = Magick::ImageList.new
imgs.from_blob file.read
# Find the largest frame
largest = imgs.map {|i| i.columns }.max
percentage = 50.0 / largest
imgs.each { |img| img.scale!(percentage) }
File.open(ARGV[1], 'wb') { |f| f.write imgs.to_blob }
}
Basically the idea is to find the largest frame, from that frame calculate the scale percentage, and scale each frame. It isn’t perfect because frames could scale differently, but it seems to work for what I’m doing.