Subject: | Photo(-data => $string) not working properly |
Date: | Tue, 6 May 2008 04:34:42 +0200 |
To: | bug-Tk [...] rt.cpan.org |
From: | Guenter Knauf <info [...] gknw.de> |
Hi,
while testing with Tk in order to display images I found that:
- on Linux Photo(-data => $string) works only with gif images
- on Win32 Photo(-data => $string) doesnt work at all
This is the error when using non-gif images:
couldn't recognize image data at /usr/lib/perl5/vendor_perl/5.8.8/i586-linux-thread-multi/Tk/Image.pm line 21.
I've written a test sample to demonstrate the issue; it can take optional arguments;
without arguments -format => gif and -data => $string is used;
tkimgx.pl
works on Linux, but on Win32 it only displays a black image.
tkimgx.pl -p
tries to use -format => png, -data = $string, but produces the error message above.
tkimgx.pl -j
tries to use -format => jpeg, -data = $string, but produces the error message above.
tkimgx.pl -p -f
tkimgx.pl -j -f
use -file = 'image.type' and work on both platforms, and:
tkimgx.pl -f
works also on Win32 with -format => gif.
Test script below and attached:
#!/usr/bin/perl -w
# CMD sample to fetch a remote web page image with LWP
# and then displays it with the Perl Tk framework.
use strict;
use LWP::UserAgent;
use Tk;
use Tk::widgets qw/PNG JPEG/;
use Tk::Dialog;
use Getopt::Std;
use vars qw/$opt_f $opt_j $opt_k $opt_p/;
getopts('fjkp');
my $version = '0.0.1';
my $url = 'http://www.perl.org/simages/lcamel.gif';
$url = 'http://www.cpan.org/misc/jpg/cpan.jpg' if ($opt_j);
$url = 'http://search.cpan.org/s/img/cpan_banner.png' if ($opt_p);
my $itype = substr($url, rindex($url, '.') + 1);
my $fname = 'tmp.' . $itype;
$itype = 'jpeg' if ($itype eq 'jpg');
my $ua = new LWP::UserAgent(agent => "$0/$version");
my $req = new HTTP::Request('GET', $url);
my $res = $ua->request($req);
die $res->status_line . "\n" if (!$res->is_success);
if ($opt_f) {
open(O,">$fname") or die "Couldn't open $fname: $!";
binmode O;
print O $res->content;
close(O) or die "Couldn't close $fname: $!";
}
my $mw = new MainWindow;
$mw->title('Tk-Image');
$mw->Label('-text' => $url)->pack;
my $im;
if ($opt_f) {
$mw->Label('-text' => $fname)->pack;
$im = $mw->Photo(-format => $itype, -file => $fname);
} else {
$im = $mw->Photo(-format => $itype, -data => $res->content);
}
$mw->Label('-image' => $im)->pack;
MainLoop;
if (($opt_f) && (!$opt_k)) {
unlink $fname;
}
Would be great if the -data option also works with png and jpeg, and consitently on all platforms.
BTW. just a guess: perhaps there's a binmode() call missing which makes the gif appear as black image on Win32?
Guenter.
#!/usr/bin/perl -w
# CMD sample to fetch a remote web page image with LWP
# and then displays it with the Perl Tk framework.
use strict;
use LWP::UserAgent;
use Tk;
use Tk::widgets qw/PNG JPEG/;
use Tk::Dialog;
use Getopt::Std;
use vars qw/$opt_f $opt_j $opt_k $opt_p/;
getopts('fjkp');
my $version = '0.0.1';
my $url = 'http://www.perl.org/simages/lcamel.gif';
$url = 'http://www.cpan.org/misc/jpg/cpan.jpg' if ($opt_j);
$url = 'http://search.cpan.org/s/img/cpan_banner.png' if ($opt_p);
my $itype = substr($url, rindex($url, '.') + 1);
my $fname = 'tmp.' . $itype;
$itype = 'jpeg' if ($itype eq 'jpg');
my $ua = new LWP::UserAgent(agent => "$0/$version");
my $req = new HTTP::Request('GET', $url);
my $res = $ua->request($req);
die $res->status_line . "\n" if (!$res->is_success);
if ($opt_f) {
open(O,">$fname") or die "Couldn't open $fname: $!";
binmode O;
print O $res->content;
close(O) or die "Couldn't close $fname: $!";
}
my $mw = new MainWindow;
$mw->title('Tk-Image');
$mw->Label('-text' => $url)->pack;
my $im;
if ($opt_f) {
$mw->Label('-text' => $fname)->pack;
$im = $mw->Photo(-format => $itype, -file => $fname);
} else {
$im = $mw->Photo(-format => $itype, -data => $res->content);
}
$mw->Label('-image' => $im)->pack;
MainLoop;
if (($opt_f) && (!$opt_k)) {
unlink $fname;
}