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

Dear Monks,

I'm writing on a program with a Tk GUI and i would like to display a png graphic that is built within my script using the GD module. Saving the picture works fine:
open(EXPORT_GRAPHIC,">picture.png"); binmode EXPORT_GRAPHIC; print EXPORT_GRAPHIC $picture->png; close EXPORT_GRAPHIC;
but displaying the picture directly in a Label doesn't work:
$new_window=$mw->Toplevel(); $frame=$new_window->Frame()->pack(); $frame->Label(-image=>$picture)->pack();
Does anyone know how to solve this problem?
  • Comment on Is it possible to display png images build with GD using Tk WITHOUT saving images first?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Is it possible to display png images build with GD using Tk WITHOUT saving images first?
by BrowserUk (Patriarch) on Oct 22, 2010 at 09:24 UTC

    Yes. But you have to convert the output of GD to base64 before supplying it it Tk.

    But be careful how fast you try to update the image. At the size in this demo if you try to update any faster than about once every 1/4 of a second and you'll never see the screen update.

    There was mention a long time ago that Tk::Photo would accept raw binary data instead of requiring it base64 encoded, but that still doesn't seem to be so.

    #! perl -slw use strict; use Tk; use Tk::PNG; use GD::Graph; use GD::Graph::bars; use MIME::Base64 qw[ encode_base64 ]; my $data = [ [qw/1st 2nd 3rd 4th 5th 6th 7th 8th 9th/], [qw/ 1 2 5 6 3 1.5 1 3 4/], ]; my $graph = GD::Graph::bars->new(400, 300); $graph->set( x_label => 'X Label', y_label => 'Y label', title => 'Some simple graph', y_max_value => 8, y_tick_number => 8, y_label_skip => 2 ) or die $!; my $gd = $graph->plot($data) or die $!; my $mw = MainWindow->new; my $png = $mw->Photo( -data => encode_base64( $gd->png ), # -data => $gd->png, -format => 'png' ); my $btn = $mw->Button( -text => 'doit', -command => sub{ $data->[ 1 ] = [ map{ rand 5 } 1 .. 9 ]; undef $graph; $graph = GD::Graph::bars->new(400, 300); $graph->set( x_label => 'X Label', y_label => 'Y label', title => 'Some simple graph', y_max_value => 8, y_tick_number => 8, y_label_skip => 2 ) or die $!; my $gd = $graph->plot($data) or die $!; $png->blank; $png->configure( -data => encode_base64( $gd->png ), # -data => $gd->png, -format => 'png' ); $mw->update; } )->pack; $mw->Label(-image => $png)->pack; $mw->repeat( 1000, sub{ $btn->invoke; $png->update } ); MainLoop;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Is it possible to display png images build with GD using Tk WITHOUT saving images first?
by kcott (Archbishop) on Oct 22, 2010 at 19:13 UTC

    The widget demo has two items under Photos and Images that may be helpful.

    -- Ken