Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Win32 Tk JPEG from memory problem

by nop (Hermit)
on Nov 11, 2001 at 02:33 UTC ( #124611=perlquestion: print w/ replies, xml ) Need Help??
nop has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I need some help with displaying a JPEG via TK using ActiveState Perl.

Pulling the image from a file works fine:
use strict; use Tk; use Tk::JPEG; my $fname = "rock.jpg"; my $win = new MainWindow; my $image = $win->Photo('-format' => 'jpeg', '-file' => $fname); $win->Label(-image=>$image)->pack; MainLoop;
But I can seem to load images from a data string (as the images will eventually be pulled from a database). That is, this doesn't work:
use strict; use Tk; use Tk::JPEG; my $fname = "rock.jpg"; open(F, $fname) or die "cant open $fname"; binmode(F); undef $/; my $jpeg = <F>; close(F); my $win = new MainWindow; my $image = $win->Photo('-format' => 'jpeg', '-data' => $jpeg); $win->Label(-image=>$image)->pack; MainLoop;
It dies with this error:
couldn't recognize image data at C:/Perl/site/lib/Tk/Image.pm line 21.
Can anyone offer advice? Am I reading the JPEG in wrong, or am I sending it to TK wrong?

Thanks

nop

Comment on Win32 Tk JPEG from memory problem
Select or Download Code
(crazyinsomniac) Re: Win32 Tk JPEG from memory problem
by crazyinsomniac (Prior) on Nov 11, 2001 at 14:23 UTC
    Not too long ago I ran into the same problem, and the only place I found a solution was pVoice and pStory. It's probably documented somewhere, but I couldn't find it at the moment (in my perl Tk documentation; it's prolly documented in the c or whatever other lang Tk is available originally in). It turns out, that when you specify a -data string, it has to be base64 encoded, and that is why the following works;
    #!/usr/bin/perl -w $^W=1; use strict; use Tk; use Tk::JPEG; use MIME::Base64 qw( encode_base64 ); my $fname = "rock.jpg"; open(F, $fname) or die "cant open $fname"; binmode(F); undef $/; my $jpeg = <F>; close(F); my $win = new MainWindow; my $image = $win->Photo( -data => encode_base64($jpeg), -format => 'JPEG', ,); $win->Label(-image=>$image)->pack; MainLoop;
    I gotta say is thanks Jouke, no wonder you got that perl GUI book deal.

    update: after some "testing" :D, it turns out you don't really need to specify -format, as it will pick it up from the -data.

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-10-20 22:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (92 votes), past polls