http://www.perlmonks.org?node_id=64376


in reply to How to handle Tk::Photo-data()?

OK, let me answer my own question so everyone here could benefit from it. This isn't documented in FAQ's or other places (like the POD-documentation of Tk). I found the answer in the Tk mailinglist archives.

For some fileformats (at least for PNG, GIF and JPEG), the data() method returns the data in base64 encoding. Also, the -data option expects the data to be base64 encoded.

To be complete, here's a snippet of code as example:
#!/usr/bin/perl -w use strict; use GD; use Tk; use Tk::JPEG; use MIME::Base64; # Create the main window my $main = MainWindow->new(); # Open a JPG image in GD my $im = GD::Image->new('feather.jpg'); # Create an empty image with the desired dimensions my $resizedim = new GD::Image(120,120); # Copy everything from $im and resize it into $resizedim $resizedim->copyResized($im,0,0,0,0,120,120,$im->getBounds()); # encode the jpeg-output of the $resizedim my $img = encode_base64($resizedim->jpeg()); # create a Photo object with the data and display it within a label my $image = $main->Photo("button", -data => $img, -format => 'jpeg'); my $label= $main->Label(-image => "button"); $label->grid(-in => $main); MainLoop;


Jouke Visser, Perl 'Adept'