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

Re: images in Perl/Tk



On Tue 19/03/2024 at 12:00, 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;
>
> 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.
> mick

Hi Mick,

With the qualification that it's been years since I've done anything much with Perl...

$ apt search perl-tk
<snip>
perl-tk/stable,now 1:804.036-1+b2 amd64 [installed,automatic]
  Perl module providing the Tk graphics library

$ apt search tkpng
<snip>
tkpng/stable 0.9-3+b1 amd64
  PNG photo image support to Tcl/Tk        <--- isn't this "not for perl"?

I couldn't find any reference to "tkpng" in

https://metacpan.org/dist/Tk

but there is:

Tk::PNG - PNG loader for Tk::Photo
(in the table there is no hyphen but wider gap)


Someone had a similar problem with Strawberry Perl on Windows.  Does this help?

https://www.perlmonks.org/?node_id=1233724

HTH
Gareth


Reply to: