Resizing Animated Gif’s with RMagick

Posted by – December 1, 2006

I 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.

3 Comments on Resizing Animated Gif’s with RMagick

  1. Alpha says:

    This might be a simpler way to find the largest frame?

    largest = imgs.map {|i| i.columns }.max

  2. That is easier! I’ll update my example. Thanks!

  3. Russ says:

    Did you ever figure out a solution for resizing the frames of an animated gif that used difference compression that allowed you to scale the width and height independently?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>