• Re: images in Perl/Tk

    From debian-user@howorth.org.uk@21:1/5 to mick.crane on Mon Mar 18 22:20:02 2024
    "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?

    You'll likely do better asking perl questions on e.g the perlmonks site.

    Would Tk::JPEG/Tk::PNG from cpan be happy with the Debian Tk and may
    possibly help recognise the file format?

    Sorry, are you suggesting using versions different to that which came
    with the version you already have? Why would you even think of doing
    that?

    If you think the version of Tk you already have isn't working then
    replace the whole thing, not just bits of it. But I'd think that was
    premature myself.

    Or is there some 'pruning' of the images, made with Gimp, needed to
    work with Tk?

    mick


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Lange@21:1/5 to mick.crane on Tue Mar 19 01:50:01 2024
    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

    Have a nice day,

    Michael

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gareth Evans@21:1/5 to mick.crane on Tue Mar 19 14:00:02 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From debian-user@howorth.org.uk@21:1/5 to mick.crane on Tue Mar 19 14:40:01 2024
    "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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)