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

how do i make my loading of images fast? if you look at fedora when browsing through pictures (, you will see the images when it first loaded is of low quality (slightly pixelized), then after awhile, the images becomes hi-quality. the images are loaded very fast! how do i achieve this using perlmagick and tk canvas? :)

Replies are listed 'Best First'.
Re: PerlMagick Fast Image Loading
by zentara (Cardinal) on Jul 14, 2008 at 13:17 UTC
    If you are using a Tk::Canvas, and you want reload speed, the thing to do is base64 encode the image and store it in a hash. Why? Because the Tk::Photo widget will automatically base64encode images, whether you load them from a file or inline variable. So.... to avoid the repeated base64encoding, do it once and save it. See Tk-thumbnail-viewer In this script, the images take a bit of time the first time a directory is loaded, as the files are encoded; but on subsequent loading, it is fast.

    That is about all you can do, unless you want to use ImageMagick to convert the images to lower quality files and reduced colors, which isn't very useful considering today's computer speed and display power.


    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: PerlMagick Fast Image Loading
by jethro (Monsignor) on Jul 14, 2008 at 11:46 UTC
    I'm guessing here that you don't mean pictures that your program had time to process beforehand (to make low-res versions of them). So your program should be able to show a fast preview of a 300k jpeg without having had access to it before.

    In that case you might experiment with the parameters units, sampling-factor, density and fuzz when you call Display. They sound like they might give you a trade-off between speed and quality.

Re: PerlMagick Fast Image Loading
by ww (Archbishop) on Jul 14, 2008 at 11:23 UTC
    when it first loaded is of low quality (slightly pixelized), then after awhile, the images becomes hi-quality

    Sounds like either .png or .jpg, properly handled, qv.

    the images are loaded very fast!
    • Define "very fast!"
    • Apt to depend on many variables: is the image file local, on a network, on a remote server?
    how do i achieve this using perlmagick and tk canvas? :)
    • Have you read the relevant documents?
    • Have you searched this site?

    And if you show us what you've tried, we may be able to offer more specific help. But the Monastery is NOT a free, code-writing machine.

      this is my code, its suppose to be a viewer/manipulator. im havent started w/ the manipulation part. to try this.. copy these codes:
      #!/usr/bin/perl -w use Tk; use Tk::Canvas; use Tk::Label; use Image::Magick; sub load_thumb; sub load_image; sub thumb_select; sub go_select_thumb; # LOAD THUMB VARIABLES # my $x; my ( $thumb_x, $thumb_y ); my $thumb_height; my $thumb_width; my $thumb_blob; my $thumb_photo; my $canvas_region; my $IM; my $thumb_selected; my @thumb_filename; # LOAD THUMB VARIABLES # my $mw = Tk::MainWindow->new(); $mw->geometry("1024x700+0+0"); my $filepath = "pictures/*"; my $canvas00 = $mw->Canvas(-width=>100,-height=>600,-background=>"whit +e"); $canvas00->place(-x=>5,-y=>5); $canvas00->bind('current','<1>',\&go_select_thumb); my $button00 = $mw->Button(-text=>"REFRESH",-command=>\&load_thumbs); $button00->place(-x=>5,-y=>610); my $canvas01 = $mw->Canvas(-width=>800,-height=>600,-background=>"whit +e"); $canvas01->place(-x=>110,-y=>5); # LOAD IMAGE VARIABLES # my $OI; # LOAD IMAGE VARIABLES # Tk::MainLoop(); sub load_thumbs { $IM = Image::Magick->new(); $IM->Read($filepath); $canvas_region = "0 0 100 " . ($#{$IM}+1) * 100; $canvas00->configure(-scrollregion=>$canvas_region); $canvas00->delete('all'); for($x=0;$x <= $#{$IM};$x++){ #Proportional Resizing ( $thumb_x, $thumb_y ) = $IM->[$x]->Get('width','height'); $thumb_filename[$x] = $IM->[$x]->Get('filename'); $thumb_height = 90; $thumb_width = $thumb_height * $thumb_x / $thumb_y; $IM->[$x]->Thumbnail(height=>$thumb_height,width=>$thumb_width +); $thumb_blob = $IM->[$x]->ImageToBlob(magick=>'xpm'); $thumb_photo = $canvas00->Photo(-data=>$thumb_blob); $canvas00->createImage(50,($x * 100)+45,-image=>$thumb_photo,- +tags=>"thumb" . $x); } #remove from memory undef $x; undef $thumb_x; undef $thumb_y; undef $thumb_height; und +ef $thumb_width; undef $canvas_region; } sub thumb_select { my @sel = $canvas00->gettags('current'); print $sel[0] . "\n"; my $index = int(substr($sel[0],5,length($sel[0])-5)); $thumb_selected = $thumb_filename[$index]; print $thumb_selected; } sub load_image { $canvas01->delete("originalphoto"); $OI = Image::Magick->new(); $OI->Read($thumb_selected); my $TOI = $OI; $TOI->Thumbnail(height=>$TOI->Get('height')*.4,width=>$TOI->Get('w +idth')*.4); my $TOI_blob = $TOI->ImageToBlob(magick=>'xpm'); my $TOI_photo = $canvas01->Photo(-data=>$TOI_blob,-format=>'xpm'); undef $TOI_blob; $canvas01->createImage($canvas01->cget('width')/2,$canvas01->cget( +'height')/2,-image=>$TOI_photo,-tags=>"originalphoto"); print "load_image\n"; } sub go_select_thumb { thumb_select; load_image; }
      create a folder named "pictures" place some 2000x2000 pics there. start the program.. click the refresh button. click on a picture. you will see that my code loads the picture, but it takes a lot of time hehe..
Re: PerlMagick Fast Image Loading
by leocharre (Priest) on Jul 14, 2008 at 19:31 UTC

    Are you talking about browsing via konqueror (KDE) or nautilus (Gnome)- or even gqview etc?

    What you describe sounds like jpeg 'progressive' scanning. That has nothing to do with the viewer, but the way the images were created.

    The other possibility is that you're actually talking about thumbnails- not the actual images. And the thumbnails are cached in ~/.thumbnails (if you're using shared cache, across apps).

    Handling image data is a ****. The faster the machines get the more we will abuse them.

      There is a third possibility. A JPEG picture tile consists of a linear combination of 64 standard tiles. If you only compute a few of them you could display an approximation of the picture somewhat faster.

      This is (if I understand it correctly) similar to the method in progressive encoding only that with this method you have to read in the whole file. With progressive encoding the coefficients of the standard tiles are grouped together so that you only have to read part of the file to display a first approximation of the picture.

      That presumes that displaying the pictures is not completely IO-bound. Just tested it on my machine, konqueror showing only thumbnails of hundreds of pictures got to 72% CPU usage, so I think showing the full pictures could be stressing out the CPU.