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

I looked up dozens of entries from google and lots of docs on Tk and GD. All of the examples show how to save images to a file, which, no surprise works! But none show how to move data from the GD format (jpeg is fine) to a Tk::Label widget...frustrating because it has to be very easy...


use Tk; use Tk::JPEG; use GD::Graph::lines3d; use strict; my $mw = new MainWindow; my @data = ( ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], [ 1203, 3500, 3973, 2859, 3012, 3423, 1230] ); my $graph = new GD::Graph::lines3d(100, 100); $graph->set( x_label => 'Day', y_label => 'Hits', title => 'Summary', ); my $gd = $graph->plot(\@data); # Works my $a = $mw->Photo('img1', -format=>'JPEG', -file=>"test.jpg"); # Doesn't work (couldn't recognize image data) #my $b = $mw->Photo('img2', -format=>'JPEG', -data=>$gd->jpeg); # Works - writes image to file (and it is the graph!) open(IMG, '>abc123.jpeg') or die $!; binmode IMG; print IMG $gd->jpeg; close(IMG); # Doesn't work, (couldn't recognize image data) #$mw->Label(-image=>$gd->jpeg) # ->pack(-fill=>'both'); # Works $mw->Label(-image=>'img1') ->pack(-fill=>'both'); MainLoop;
I know it is my fault, but it seems that I have asked for a JPEG format and then passed a JPEG format ??? Is this what they call "weak typing"?

The $gd->jpeg has a value starting with:
ÿØÿà..JFIF..........ÿþ.>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality\n" (got it from the debugger)

thanks for any help !!!

Edit kudra, 2001-09-26 Added code tags

Replies are listed 'Best First'.
Re: passing anything from GD to Tk ???
by Jouke (Curate) on Sep 28, 2001 at 11:04 UTC
    Hmm..I suppose this is becoming something for the GUI FAQ. This question has been asked a few days ago on the ptk mailinglist, which I responded with this suggestion:

    If you use MIME::Base64, and base64-encode the data, before feeding it to Tk, you can use the image you created with GD in Tk. I've used it in pVoice 0.01, where I resize a JPG with GD before using it in a Tk button.

    HTH,

    Jouke Visser, Perl 'Adept'
    Using Perl to help the disabled: pVoice and pStory
      Today, I actually stumbled across why this is ...because it is legacy TCL, which expects everything to be a string and Base64 encodes that...but you probably already know this... (BTW, it would be good for the FAQ)
(mitd) Re: passing anything from GD to Tk ???
by mitd (Curate) on Sep 26, 2001 at 10:27 UTC
    Bad news I'm afraid.
    Excerpt from Tk::JPEG README:
    ... The support for -data and passing options to read and write is incomplete. ...
    I tried PNG which appears to suffer from the same fate.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      Thanks !!!
      Anyone know if there is another compatible format to use to show the chart in a Tk widget?

      Would the an old version of GD that suppored GIF help? Is this just a JPEG issue?

      Is there something that can replace GD?

      -john