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

Hello Monks, it has been a awhile since I was here to ask a question, and I have been searching for an answer for this one for a while with no luck. Any help would be greatly appreciated.

I am putting together a TK script for loading various jpeg files so I can scroll through and rename the pictures taken from my camera in a little simpler way. I am running into a little problem though; my camera takes pictures with a sie of 2048x1360 and my monitor is 1280X800. This means that the jpeg images don't show properly on my screen when I make them in a TK object. Anyone know how to generically reduce the image size (say always show at 25% or 50% actual size)?.
#!/usr/bin/perl -w use strict; use Tk; use Tk::Photo; use Tk::JPEG; my $file="c:/pictures/2005/guns/DSC00101.JPG"; my $w=new MainWindow; my $image=$w->Photo(-file=>"$file", -format => 'jpeg'); my $b = $w->Button( -image => $image )->pack(); $b->configure(-image => $image); &MainLoop;
Thanks in advance to all who respond! -Kevin

Replies are listed 'Best First'.
Re: Resizing Jpegs in TK
by Zaxo (Archbishop) on Jan 17, 2005 at 06:02 UTC

    The pod for Tk::Photo mentions a -subsample property of an image object. It looks like setting that to 2 will give you a 50% reduction in image size.

    After Compline,
    Zaxo

      Zaxo, Thanx for replying, I attempted to use -subample, but I can only seem to get it to work when using method (copy). It still appears to try and put a full size image together and then copy a portion of it. This still creates an odd mish-mash looking picture (although it is smaller). I tried using various values for subsample. (1) (2) (2,2) (3,3) (4,4) (3) (4) none of them appear to work properly. Am I doing something wrong?
      #!/usr/bin/perl -w use Tk; use Tk::Photo; use Tk::JPEG; use strict; my $file="c:/pictures/2005/guns/DSC00101.JPG"; my $w=new MainWindow; my $image1=$w->Photo(-file=>"$file", -format => 'jpeg'); my $image=$w->Photo(); $image->copy($image1,-subsample=>4); $b = $w->Button( -image => $image )->pack(); $b->configure(-image => $image); &MainLoop;


      -Kevin
        This code of yours works fine on Linux.

        I'm not really a human, but I play one on earth. flash japh

        Works fine on XP/5.85 too.


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.
Re: Resizing Jpegs in TK
by zentara (Cardinal) on Jan 17, 2005 at 12:46 UTC
    Usually, you use GD or ImageMagick to do any graphic manipulation. Here is a way with GD. I don't use Windows, so you should see which is easiest for you to install, GD or IM, then ask again when you have one of them.
    #!/usr/bin/perl -w use strict; use GD; use Tk; use Tk::JPEG; use MIME::Base64; # Create the main window my $main = MainWindow->new(); # Open a JPG image in GD my $im = GD::Image->new('zen16.jpg'); # Create an empty image with the desired dimensions my $resizedim = new GD::Image(80,80); # Copy everything from $im and resize it into $resizedim $resizedim->copyResized($im,0,0,0,0,80,80,$im->getBounds()); # encode the jpeg-output of the $resizedim my $img = encode_base64($resizedim->jpeg()); # create a Photo object with the data and display it within a label my $image = $main->Photo("button", -data => $img, -format => 'jpeg'); my $label= $main->Label(-image => "button"); $label->grid(-in => $main); MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      Thanks for the tip. This one works now!
Re: Resizing Jpegs in TK
by ldln (Pilgrim) on Jan 17, 2005 at 19:05 UTC
    You may want to take a look at irfanview which is got quite powerful command-line support for manipulating images. It can be run in silent mode, from perl, and it all works quite well.

    To resize image, use the resize-switch:
    (From the help docs)
    /resize=(w,h) - resize input image to w (width) and h (height)