Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Matts (1087)

  (email not shown publicly)

I work for MessageLabs [] in Toronto, ON, Canada. I write spam filters, MTA software, high performance network software, string matching algorithms, and other cool stuff mostly in Perl and C.

Journal of Matts (1087)

Tuesday August 22, 2006
11:17 AM

Image resizing for a gallery using Imager

[ #30721 ]

I don't know if I've lamented on this blog before about the performance of image resizing (using Imager) for my gallery. It takes about 5 or 6 seconds to fully resize an image from 3000x2000 down to 800 pixels wide. That's because Imager uses a bicubic (I think) smoothing algorithm to make the resize look nice. If you use the "preview" quality type it's MUCH faster, but the results are horrible.

Ever had one of those moments lying in bed where you think: "I wonder if this would work?". I used to have those and forget them, so I now keep a pad of paper and pen by the bed and write them down. This morning it was the following algorithm:

    # resize $file of type $type to $size, put data in $out
    my $image = Imager->new;
    $image->open(file => $file)
        or die $image->errstr();
    my($w, $h) = ($image->getwidth(), $image->getheight());
    my $doublesize = $size < (($w > $h ? $w : $h)/2);
    if ($doublesize) {
        $self->log(LOGINFO, "Doing an initial shrinkage in preview mode");
        my $thumb = $image->scale(qtype => "preview",
                                  $w > $h ? (xpixels => $w/2)
                                          : (ypixels => $h/2)
        $image = $thumb;
    my $thumb = $image->scale(qtype => "normal",
                               $w > $h ?
                                   (xpixels => $size)
                                 : (ypixels => $size)
    if ($doublesize) {
        $self->log(LOGINFO, "sharpening $thumb");
        # Sharpen a bit with a convolve filter
            coef => [-0.2, 1, -0.2]
    $thumb->write(data => $out, type => $type)
        or die "Cannot write to scalar: ", $thumb->errstr;

Resizes now happen within about a second or so. Not as fast as Epeg, but the results are a LOT prettier.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • That’s a nice snippet. It would have been easier to follow a prose description though: “if the image is more than twice as large as the target resolution, use preview mode to quickly cut it in half before downsizing normally, and then sharpen the result a bit afterwards.”

    Anyway, I’m filing this idea away.

    • sorry - I should have added a description.

      Still, it made you read the code :-)
  • Memories supports resizing with Imager if it has to, but prefers Image::Imlib2, which is lightning-fast for image resizes. So much so that it generates the resized versions of pictures on the fly without noticable performance degradation.
    • I haven't. Harder to install? I guess it requires libimlib2...
      • I installed it from distro packages and of course didn't have any trouble, on both Debian and CentOS.
    • I was recently planning to switch an app from ImageMagick to Imlib2 for thumbnails and wrote a benchmark to see how much improvement I would get. I just added Imager for comparison:

      Rate imager magick preview imlib2
      imager 4.92/s -- -2% -64% -82%
      magick 5.04/s 2% -- -63% -81%
      preview 13.5/s 174% 168% -- -50%
      imlib2 26.8/s 445% 432% 99% --

      So, Imlib2 is faster than even Imager's preview mode. Of course Imager does a lot of thin

      • Use <ecode> tags :-)

        Thanks - looks like I need to investigate Imlib2.

        I suppose working on Win32 is too much to ask.
  • The next release of Imager will add a new scaling qtype that's faster than normal but better quality than preview. It's still slower than I like.