Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

95 views
Skip to first unread message

Josef Moellers

unread,
Oct 15, 2008, 9:54:07 AM10/15/08
to
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

zentara

unread,
Oct 16, 2008, 6:53:08 AM10/16/08
to
On Wed, 15 Oct 2008 15:54:07 +0200, Josef Moellers
<josef.m...@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

Josef Moellers

unread,
Oct 16, 2008, 9:03:51 AM10/16/08
to
zentara wrote:
> On Wed, 15 Oct 2008 15:54:07 +0200, Josef Moellers
> <josef.m...@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 ;-)

0 new messages