How to display an Image::Magick image in a Tk::Canvas? - Perl

This is a discussion on How to display an Image::Magick image in a Tk::Canvas? - Perl ; Hi, I need to display a PGM image in a Perl/TK window. I tried using Tk::Photo, but it doesn't want my PGM data (obtained from scanimage): my $p = $top->Photo(-data => $image, -format => 'PGM'); However, I can load it ...

+ Reply to Thread
Results 1 to 3 of 3

How to display an Image::Magick image in a Tk::Canvas?

  1. Default How to display an Image::Magick image in a Tk::Canvas?

    Hi,

    I need to display a PGM image in a Perl/TK window.

    I tried using Tk::Photo, but it doesn't want my PGM data (obtained from
    scanimage):
    my $p = $top->Photo(-data => $image, -format => 'PGM');

    However, I can load it into Image::Magick:
    $image->BlobToImage($pgm);

    How can I display the image loaded in Image::Magick?
    I'd like to avoid storing the image on disk, as it will be sent to a
    printer right away, it's just for previewing purposes.

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html

  2. Default Re: How to display an Image::Magick image in a Tk::Canvas?

    On Wed, 15 Oct 2008 15:54:07 +0200, Josef Moellers
    <josef.moellers@fujitsu-siemens.com> wrote:

    >I need to display a PGM image in a Perl/TK window.
    >I tried using Tk::Photo, but it doesn't want my PGM data (obtained from
    >scanimage):
    >my $p = $top->Photo(-data => $image, -format => 'PGM');
    >
    >However, I can load it into Image::Magick:
    >$image->BlobToImage($pgm);
    >
    >How can I display the image loaded in Image::Magick?
    >I'd like to avoid storing the image on disk, as it will be sent to a
    >printer right away, it's just for previewing purposes.
    >Josef


    Load the blob with ImageMagick as PGM, then chnage the magick
    to gif, jpg, or png. Gif is built-in so maybe use it.

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Image::Magick;
    use Tk;
    #use Tk::JPEG;
    #use Tk::PNG;
    use MIME::Base64;

    #fake a PGM then convert it to gif
    my $image = Image::Magick->new(
    size => "600x600",
    );
    $image->Read("xc:white");
    $image->Draw(
    primitive => 'line',
    points => "300,100 300,500",
    stroke => '#600',
    );
    # set it as PGM
    $image->Set(magick=>'pgm');

    #your pgm is loaded here, now change it to gif or whatever
    $image->Set(magick=>'gif');
    my $blob = $image->ImageToBlob();

    # Tk wants base64encoded images
    my $content = encode_base64( $blob ) or die $!;

    my $mw = MainWindow->new();
    my $tkimage = $mw->Photo(-data => $content);
    $mw->Label(-image => $tkimage)->pack(-expand => 1, -fill => 'both');
    $mw->Button(-text => 'Quit', -command => [destroy => $mw])->pack;
    MainLoop;
    __END__


    zentara


  3. Default Re: How to display an Image::Magick image in a Tk::Canvas?

    zentara wrote:
    > On Wed, 15 Oct 2008 15:54:07 +0200, Josef Moellers
    > <josef.moellers@fujitsu-siemens.com> wrote:
    >
    >> I need to display a PGM image in a Perl/TK window.
    >> I tried using Tk::Photo, but it doesn't want my PGM data (obtained from
    >> scanimage):
    >> my $p = $top->Photo(-data => $image, -format => 'PGM');
    >>
    >> However, I can load it into Image::Magick:
    >> $image->BlobToImage($pgm);
    >>
    >> How can I display the image loaded in Image::Magick?
    >> I'd like to avoid storing the image on disk, as it will be sent to a
    >> printer right away, it's just for previewing purposes.
    >> Josef

    >
    > Load the blob with ImageMagick as PGM, then chnage the magick
    > to gif, jpg, or png. Gif is built-in so maybe use it.
    >
    > #!/usr/bin/perl

    [ ... ]
    > __END__
    >
    >
    > zentara


    Works like a charm. Now I owe you two ;-)

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html

+ Reply to Thread