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

Good day Monks. I am trying to use Tk to write an ap that will go through a set of jpgs in a directory, showing them in a window. I would like to scale these pics to fit the window.

I have managed to write code that will step thru and display the jpgs, but I can't quite figure out how to scale them. I think I would use the copy method along with a shrink or zoom argument. But I'm not quite proficient enough with Tk to know how to read in one image, copy it to another resized, then display the second one. Can someone help me out? Existing code is below.

TIA.....Steve

#!/usr/bin/perl -w use strict; use Tk; use Tk::JPEG; my $c; my $dir = "c:/temp"; my @fl = ("01.jpg","05.jpg"); my $main = new MainWindow; nextp(); my $nxt = $main->Button('-text' => "Next", '-command' => \&nextp)->pac +k(); $main->Button(-text => 'exit', -command => sub{destroy $main} )->pack(-anchor => 'e'); MainLoop; sub loadpic { my $filename = shift; my $size; my $orientation; $main -> Photo('img', -file => $filename); my ($h,$w) = ($main->height, $main->width); # # how to scale the pic? # $c = $main->Label('-image' => 'img')->pack; return; } sub nextp { my $f = shift(@fl); loadpic("$dir/$f"); return; }

Replies are listed 'Best First'.
Re: Tk sizing a pic to fit a window
by zentara (Cardinal) on Mar 19, 2006 at 00:20 UTC
    You have a couple of beginner design errors in your code. We all went thru it. :-) First you are putting the photo into the $main window, without out setting a size for the main window, or the desired size for the photo. And the way you did it, every load, you created a new widget (which causes memory gains).

    Also you really can't do what you want with just the plain Tk::Photo object (read perldoc Tk::Photo ). You can zoom or shrink, but can't set an absolute destination size. Furthermore, the Tk::Photo object won't give you the dimensions of the photo. If you had those, then you could compare your display window size to your photo size(maybe use Image::Info), then do some calculations to decide whether you need to shrink or zoom the photo, and by what factor. A big hassle. The Tk::Photo object is not that powerful. So here is a basic example to show you a shrink by 4, just to actually answer your question on resizing a Tk photo .

    Now you can use GD or ImageMagick to do the resizing, to an absolute size. There are some glitches to work out, like preventing stretching and maintaining aspect ratio. But this ImageMagick with Tk script, will show you the idea. There is a small glitch the way I have it setup. The first photo won't automatically display, probably because of the time it takes for ImageMagick to get setup. I used a button->invoke to get around it.


    I'm not really a human, but I play one on earth. flash japh
      Dude, thanks! That was a giant help. I didn't know you could use Image::Magick and Tk togehter.

      Steve

Re: Tk sizing a pic to fit a window
by hesco (Deacon) on Mar 18, 2006 at 20:34 UTC
    I've never coded in Tk, so this may not apply. In fact, I've never coded against Image::Magic, but I understand that that module is designed to do exactly that. I have used its command line tools, which will convert images from one format to another and resize images as needed.

    -- Hugh