Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by Jouke (Curate)
on Mar 14, 2001 at 16:38 UTC ( #64370=perlquestion: print w/replies, xml ) Need Help??

Jouke has asked for the wisdom of the Perl Monks concerning the following question:

After a reply on my question of yesterday (Resizing images && Tk), which suggested to use GD generated data in Tk::Photo objects, I started fiddling with the data() method of Tk::Photo (and its companion  -data => $imagedata)

I can't figure out what kind of data this method returns. Probably it's a packed or unpacked set of raw imagedata. The data generated by Tk::Photo->data(-format => 'jpeg') is a little bit more than 6 times larger than the data generated by  GD::Image->jpeg(). This 'little bit' could be explained by the identifier-string that's included in the image-data of a JPG.

Does anyone know how to convert between the data generated by these two methods?

Jouke Visser, Perl 'Adept'

Replies are listed 'Best First'.
Re: How to handle Tk::Photo-data()?
by Jouke (Curate) on Mar 14, 2001 at 17:30 UTC
    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'
      Jouke, I tried your code, and it seems to work well except the image (jpg) I use keeps showing up as all black. I used your code verbatim. Is there something I need to do differently on a Windows system? What OS did you use? Help from anyone?
        I use this piece of code in pVoice (see the code for that) and that works on both Win32 and Un*x platforms. So I think your code or your image may be the problem. Please compare your code to the code of pVoice and see what the problem is.

        Jouke Visser, Perl 'Adept'
        Using Perl to help the disabled: pVoice and pStory

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://64370]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2019-09-16 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (196 votes). Check out past polls.

    Notices?