in reply to Re^2: Tk:Photo -data with binary data?
in thread Tk:Photo -data with binary data?

Well, it will very much depend on the image format of the data (eg. "gif", or "jpg", or "png", or whatever).  Tk only supports certain image formats:
IMAGE FORMATS The photo image code is structured to allow handlers for additiona +l image file formats to be added easily. The photo image code mainta +ins a list of these handlers. Handlers are added to the list by register +ing them with a call to Tk_CreatePhotoImageFormat. The standard Tk distribution comes with handlers for XBM, XPM, BMP, JPEG, PNG and PPM/PGM formats, which are automatically registered on initializat +ion.

and in the next paragraph ...

Usually this will find the correct handler, but if it doesn't, the user may give a format name with the -format option to specify which handler to use.

I'm assuming you know what the image format is of the image you have in memory.  Why not just try displaying the image yourself?  As the Tk documentation illustrates, you can use:

my $img = $mw->Photo(-data => $data);

and if necessary supply -format => FORMAT (for one of the valid IMAGE FORMATS above).

Have you tried doing that yet?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^4: Tk:Photo -data with binary data?
by BrowserUk (Patriarch) on Nov 28, 2006 at 23:38 UTC

    Yes, I've tried that too. The following example works if you switch the comment cards to encode the data base64, but not without. I've also tried various different formats that GD support (.jpg, .gif). The result is always couldn't recognize image data at c:/Perl/site/lib/Tk/Image.pm line 21, which is annoying because it can read that same data back from a file, with or without a format hint.

    It seems strange that is can read the data from a file, but not directly from memory. If the -file option would take a file handle instead of a name, I could throw the data into a ramfile, but it doesn't :(

    The time taken to convert the image to base64 to pass it into tk--where it is presumably converted straight back again--thrashes the cpu to death and really slows things down of you are trying to update the image quickly.

    #! 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; __END__ couldn't recognize image data at c:/Perl/site/lib/Tk/Image.pm line 21.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.