| 1 |
#!perl -w |
| 2 |
use strict; |
| 3 |
use Tk; |
| 4 |
use Tk::Photo; |
| 5 |
use MIME::Base64; |
| 6 |
use Tk::PNG; |
| 7 |
use Imager; |
| 8 |
|
| 9 |
my $image = Imager->new(xsize=>100, ysize=>100); |
| 10 |
|
| 11 |
# draw something simple here, you'll probably do something more complex |
| 12 |
$image->box(filled=>1, color=>'blue'); |
| 13 |
$image->box(filled=>1, color=>'red', |
| 14 |
xmin=>20, ymin=>20, xmax=>79, ymax=>79); |
| 15 |
|
| 16 |
my $image_data; |
| 17 |
$image->write(data =>\$image_data, type=>'png') |
| 18 |
or die "Cannot save image: ", $image->errstr; |
| 19 |
|
| 20 |
# supplying binary data didn't work, so we base64 encode it |
| 21 |
$image_data = encode_base64($image_data); |
| 22 |
|
| 23 |
my $main = MainWindow->new; |
| 24 |
my $tk_image = $main->Photo(-data => $image_data); |
| 25 |
$main->Label(-image=>$tk_image)->pack; |
| 26 |
MainLoop; |
| 27 |
|
| 28 |
=head1 NAME |
| 29 |
|
| 30 |
=for stopwords tk-photo.pl |
| 31 |
|
| 32 |
tk-photo.pl - display an Imager image under Tk |
| 33 |
|
| 34 |
=head1 SYNOPSIS |
| 35 |
|
| 36 |
$ perl tk-photo.pl |
| 37 |
|
| 38 |
=head1 DESCRIPTION |
| 39 |
|
| 40 |
Simple code to make a Tk::Photo object from an Imager image. |
| 41 |
|
| 42 |
This works by: |
| 43 |
|
| 44 |
=over |
| 45 |
|
| 46 |
=item 1. |
| 47 |
|
| 48 |
write the image data to a scalar in PNG format |
| 49 |
|
| 50 |
=item 2. |
| 51 |
|
| 52 |
Base64 decode the data |
| 53 |
|
| 54 |
=item 3. |
| 55 |
|
| 56 |
read() it into the photo object, supplying the Base64 encoded data to |
| 57 |
the C<-data> parameter. |
| 58 |
|
| 59 |
=back |
| 60 |
|
| 61 |
=head1 REVISION |
| 62 |
|
| 63 |
$Revision$ |
| 64 |
|
| 65 |
=head1 AUTHOR |
| 66 |
|
| 67 |
Tony Cook <[email protected]> |
| 68 |
|
| 69 |
=cut |