File: tk-photo.pl

package info (click to toggle)
libimager-perl 0.50-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,120 kB
  • ctags: 2,907
  • sloc: ansic: 21,092; perl: 15,461; makefile: 56
file content (67 lines) | stat: -rw-r--r-- 1,178 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!perl -w
use strict;
use Tk;
use Tk::Photo;
use MIME::Base64;
use Tk::PNG;
use Imager;

my $image = Imager->new(xsize=>100, ysize=>100);

# draw something simple here, you'll probably do something more complex
$image->box(filled=>1, color=>'blue');
$image->box(filled=>1, color=>'red', 
	    xmin=>20, ymin=>20, xmax=>79, ymax=>79);

my $image_data;
$image->write(data =>\$image_data, type=>'png')
  or die "Cannot save image: ", $image->errstr;

# supplying binary data didn't work, so we base64 encode it
$image_data = encode_base64($image_data);

my $main = MainWindow->new;
my $tk_image = $main->Photo(-data => $image_data);
$main->Label(-image=>$tk_image)->pack;
MainLoop;

=head1 NAME

tk-photo.pl - display an Imager image under Tk

=head1 SYNOPSIS

  $ perl tk-photo.pl

=head1 DESCRIPTION

Simple code to make a Tk::Photo object from an Imager image.

This works by:

=over

=item 1.

write the image data to a scalar in PNG format

=item 2.

Base64 decode the data

=item 3.

read() it into the photo object, supplying the Base64 encoded data to
the C<-data> parameter.

=back

=head1 REVISION

$Revision: 830 $

=head1 AUTHOR

Tony Cook <tony@imager.perl.org>

=cut