Simple Photo Rotations
Using standard Photo methods, it’s possible to rotate an image 90 degrees clockwise, 90 degrees counter-clockwise, or flip it 180 degrees.[6]
Once encapsulated in a module—call it Tk::PhotoRotateSimple—we can showcase its capabilities with this code, the result of which is shown in Figure 17-22.
use Tk; use Tk::PhotoRotateSimple; use subs qw/rotate/; use strict; my $mw = MainWindow->new; my $p = $mw->Photo(-file => Tk->findINC('Xcamel.gif')); rotate 'Original'; rotate 'flip'; rotate 'l90'; rotate 'r90'; MainLoop; sub rotate { my $direction = shift; my $f = $mw->Frame(qw/-width 100 -height 100/)->pack(qw/-side left/); $f->packPropagate(0); $f->Label(-text => $direction)->pack; my $i = $f->Label(-image => $p)->pack(qw/-expand 1 -fill both -anchor c/); return if $direction eq 'Original'; my $tmp = $mw->Photo; $tmp->copy($p); $tmp->rotate_simple($direction); $i->configure(-image => $tmp); }
$p
is our friendly camel Photo object. Using it as
the original, we call rotate
to rotate the image
three times: 180 degrees, left 90 degrees, and right 90 degrees. The
first call to rotate
does no rotation, it just
displays the original Photo and returns. The
rotate_simple
method rotates the actual Photo, so
we make a temporary copy in order to preserve the original. Then call
rotate_simple
with flip
,
l90
, or r90
.
So much for the user’s point of view; let’s see the actual module.
Figure 17-22. Rotate window
The first thing to note is that we are extending the class Tk::Photo ...
Get Mastering Perl/Tk now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.