shimmin has asked for the wisdom of the Perl Monks concerning the following question:

I'm working with ActivePerl build 810 on an XP box. I want to resize a large image so that it will fit on the screen, with a little resampling, and then show it in a Tk::Label. $imgfile and $imgwidth are the file containing the image and the width to resize it to. $display is the Tk::Photo that contains the on-screen image. My first go worked fine:
sub rerender {
  `i_view32.exe .\\$imgfile /resample=($imgwidth,) /aspectratio /convert=".\\disp.png"`;
  $display->configure(-file => 'disp.png');
}
This uses an external program (irfanview) to do the dirty work, saves the downsized image to a temp file, and then reads it into the Tk::Photo. Thinking it could be faster w/o writing and readign the temp file, I did this using the Image::Magick module:
sub rerender {
  my $image = Image::Magick->new();
  $image->Read($imgfile);
  $image->Resize(width => $imgwidth, height => $imgwidth * 1.5, filter => Box);
  my $blob = $image->ImageToBlob();
  $display->configure(-format => 'png', -data => encode_base64($blob));
}
Which also works, but takes twice as long as the first version. Where do you think the time being spent? How could the image be produced and displayed any faster?

Replies are listed 'Best First'.
Re: displaying an image
by Zaxo (Archbishop) on Jul 01, 2004 at 15:21 UTC

    How about caching each resized image as a file whose name or path encodes the size? That way the expensive stuff only needs to be done once.

    After Compline,
    Zaxo