[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: images in Perl/Tk



"mick.crane" <mick.crane@gmail.com> wrote:
> On 2024-03-19 00:42, Michael Lange wrote:
> > Hi,
> > 
> > On Mon, 18 Mar 2024 19:23:39 +0000
> > "mick.crane" <mick.crane@gmail.com> wrote:
> >   
> >> I try to load images with Perl/Tk but there is message,
> >> "couldn't recognize data in image file "test.jpeg" at
> >> /usr/lib/x86_64-linux-gnu/perl5/5.38/Tk/Image.pm line 21"
> >> I've tried different images/pngs/jpgs with same error.
> >> images load OK in other viewers.
> >> Installed tkpng with apt.
> >> 
> >> Anybody successfully used images with Perl/Tk?
> >> Would Tk::JPEG/Tk::PNG from cpan be happy with the Debian Tk and
> >> may possibly help recognise the file format?
> >> Or is there some 'pruning' of the images, made with Gimp, needed to
> >> work with Tk?
> >> 
> >> mick
> >>   
> > 
> > I never used Tk with Perl, but first, you do not need tkpng for png
> > images anymore, pngs have been supported natively by Tk for years. 
> > Maybe
> > tkpng is broken (as at least some versions of img::png that came
> > with debian have been)?
> > Second, Tk does not support jpegs without third-party
> > extensions; probably img::jpeg can help, so you could try to
> > install libtk-img with apt and then do the Perl equivalent of
> > 
> >    package require img::jpeg  
> 
> Made an image, saved as .bmp, gif, ppm, pnm, tif, png, jpg
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Image::Imlib2;
> my $image=Image::Imlib2->new(400,400);
> $image->has_alpha(1);
> $image->set_color(255,0,0,255);
> $image->fill_rectangle(0,0,400,400);
> $image->save('./images/red.png');
> 
> try to display image in Perl/Tk
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Tk;
> my $mw = MainWindow->new;
> $mw->title("Test");
> my $image = $mw->Photo(-file => "images/red.bmp");
> my $image_label = $mw->Label(-image => $image)->pack;
> MainLoop;

There's a bug in your program above, when used for PNG or JPEG. It's a
perl error and I expect you would have got an answer on perlmonks as I
suggested.

The error is described on
https://docstore.mik.ua/orelly/perl3/tk/ch17_01.htm

Look for "Notice the Photo image formats JPEG, PNG, and TIFF—not part
of the Perl/Tk distribution—must be specifically imported."


> Only the .bmp file displays other files have error.
> 
> "Uncaught exception from user code:
> 	couldn't recognize data in image file "images/red.png" at 
> /usr/lib/x86_64-linux-gnu/perl5/5.38/Tk/Image.pm line 21.
> 	Tk::Image::new("Tk::Photo", MainWindow=HASH(0x560632dc9098),
> "-file", "images/red.png") called at 
> /usr/lib/x86_64-linux-gnu/perl5/5.38/Tk/Image.pm line 63
> 	Tk::Image::__ANON__(MainWindow=HASH(0x560632dc9098), "-file", 
> "images/red.png") called at ./test-image.pl line 8"
>
> I've installed
> Tk, libgraphics-magick-perl, libimage-imlib2-perl, tkagif, tkblt,
> tklib, libtk-img, tkpng.
> 
> Now that I know .bmp works I can use that.
> Wonder tho' why others don't display, Bookworm and Trixie.
> If there is some other module to include I can't discover what it's
> name is as yet.
> Tk::Photo is a module but seems to be included in Perl/Tk
> I'm not really understanding if the Perl/Tk is a rewrite or an
> interface to Tk modules.

You might want to read the book! :) Or even the CPAN entry for Tk if
you want to understand more.


Reply to: