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

I’m experimenting around with Tk using StrawberryPerl, and I’m practicing/experimenting with displaying images. For some reason, I can use Tk::Photo to load and display GIF’s, but I can’t seem to get it to work with JPG’s or PNG’s. I’m sure that there are probably other modules available. I wouldn’t mind experimenting with them too. However, I’m trying to give Tk a thorough trial before I jump ship for something else.

I’m running Perl 5.28.2 built for MSWin32-x64-multi-thread via StrawberryPerl. Here is my code
use strict; use warnings; use Tk; my $mw = MainWindow->new; my $lbl = $mw->Label; my $photo = $mw->Photo(-file=>”samplejpg1.jpg”); $lbl->configure(-image=>$photo); $lbl->pack; MainLoop;

If I let Tk try to auto detect the type, then I get couldn’t recognize data in image file “samplejpg1.jpg” at C:/Strawberry/Perl/site/lib/Tk/Image.pm line 21. If I specify a format, using -format=>”jpg” then I get image file format “jpg” is not supported at C:/Strawberry/Perl/site/lib/Tk/Image.pm line 21.

Same thing happens when I switch out the JPEG for the PNG file. But if I swap out the file for a GIF, then it displays as expected. I suppose it’s possible that I have bad test images, but I’m not sure that is likely. Aren’t these pretty standard/stable image types?

Here are my questions:
  1. Is there anyway my Tk installation could be incomplete? I used the CPAN client for the whole thing and didn’t install anything else. I was half expecting to have to install additional libraries, but it works with other forms and fields.
  2. Am I using the modules correctly? Or did I forget some basic step in my code?
  3. Is there some way to get this working, specifically using Tk::Photo?
  4. Are there other packages/modules that offer GUI toolkits and/or image manipulation?

Replies are listed 'Best First'.
Re: Displaying PNG and JPG images using Tk on Windows
by pryrt (Abbot) on May 13, 2019 at 19:35 UTC

    I replicated your error with your code. I looked at the test suite for Tk in the t/photo.t, and they include use Tk::PNG; use Tk::JPEG; (or equivalent conditional require's), so I successfully tried code with the two extra use statements:

    use strict; use warnings; use Tk; use Tk::PNG; use Tk::JPEG; my @arr; push @arr, { name => 'sample.gif', type => 'image/gif'}; push @arr, { name => 'sample.png', type => 'image/png'}; push @arr, { name => 'sample.jpg', type => 'image/jpeg'}; foreach my $fh (@arr) { my $f = $fh->{name}; my $t = $fh->{type}; eval { my $mw = MainWindow->new; my $lbl = $mw->Label; my $photo = $mw->Photo(-file=>$f); #, -format=>$t ); $lbl->configure(-image=>$photo); $lbl->pack; MainLoop; 1; } or do { warn "here: $@\n"; }; }

    update: I was curious about the -format, since the mime-types I had tried (based on commented-out code) didn't work. the t/photo.t showed method $photo->formats(), so I printed those => jpeg, png, bmp, xpm, xbm, gif, PPM. So, to get -format to work with my code, it should be as follows (which still works):

    use strict; use warnings; use Tk; use Tk::PNG; use Tk::JPEG; my @arr; push @arr, { name => 'sample.gif', type => 'gif'}; push @arr, { name => 'sample.png', type => 'png'}; push @arr, { name => 'sample.jpg', type => 'jpeg'}; foreach my $fh (@arr) { my $f = $fh->{name}; my $t = $fh->{type}; eval { my $mw = MainWindow->new; my $lbl = $mw->Label; my $photo = $mw->Photo(-file=>$f, -format=>$t ); print join ", ", $photo->formats(), "\n"; $lbl->configure(-image=>$photo); $lbl->pack; MainLoop; 1; } or do { warn "here: $@\n"; }; }
      Success. Thanks for the help. Though it would be helpful if that info was included in the error message.
Re: Displaying PNG and JPG images using Tk on Windows
by Discipulus (Canon) on May 13, 2019 at 20:14 UTC
    hello skleblan

    pryrt is right! look at Tk::JPEG documentation:

    > This is an extension for Tk which supplies JPEG format loader for Photo image type

    In the Tk::Photo docs is said which image format are supported:

    > At present, only GIF, XBM, XPM, BMP, JPEG, PNG and PPM/PGM formats are supported

    My picwoodpecker Tk program handles all formats if you are interested. It also use XPM to produce the icon of the program itself.

    > 2 - Am I using the modules correctly? Or did I forget some basic step in my code?

    Think more like Photo is a container. You must instruct it to know what it holds or, only for GIFs, if it can handle automatically.

    > 4 - Are there other packages/modules that offer GUI toolkits and/or image manipulation?

    I'm a big fan of GD as you can see in the above linked program of mine. Among GUI toolkit you can look at Prima

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks for the feedback. I’ll have to check out GD and Prima as well.