Image resizing for a gallery using Imager

Matts on 2006-08-22T16:17:40

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 $thumb->filter( type=>'conv', 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.


Reading code

Aristotle on 2006-08-22T19:55:03

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.

Re:Reading code

Matts on 2006-08-22T20:09:13

sorry - I should have added a description.

Still, it made you read the code :-)

Tried Imlib2?

Simon on 2006-08-22T22:04:14

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.

Re:Tried Imlib2?

Matts on 2006-08-22T22:07:19

I haven't. Harder to install? I guess it requires libimlib2...

Re:Tried Imlib2?

stu42j on 2006-08-23T22:01:24

I installed it from distro packages and of course didn't have any trouble, on both Debian and CentOS.

Re:Tried Imlib2?

stu42j on 2006-08-23T22:10:26

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 things that Imlib2 doesn't but for thumbnails, Imlib2 is the fastest.

P.S. Why can't I use pre tags here?

Re:Tried Imlib2?

Matts on 2006-08-23T22:16:41

Use <ecode> tags :-)

Thanks - looks like I need to investigate Imlib2.

I suppose working on Win32 is too much to ask.

Image resizing

tonyc on 2006-08-23T23:44:49

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.