Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tk::Photo -data format

92 views
Skip to first unread message

Oliver 'ojo' Bedford

unread,
Oct 14, 2009, 4:03:13 PM10/14/09
to
Hi,

I am supplying image data (BMP or PNG) via the -data option to Tk::Photo.
Is it necessary for the data string to be BASE65-encoded?

Own experiments suggest yes, my understanding of the (german)
man-page was different (that I could supply directly the contents of a
file).

Background: I want to transform the image in memory using Imager
or something similar and then display it using Tk::Photo.

Oliver

Steve C

unread,
Oct 14, 2009, 4:57:58 PM10/14/09
to


I found this code in StyledButton.pm

use Tk;
use Tk::PNG;
use Tk::JPEG;
use Tk::Photo;
use MIME::Base64;

sub _setGDImage {
my ($self, $image) = @_;
return $self->Photo(-data => encode_base64($image->png()), -format => 'png');
}

Oliver 'ojo' Bedford

unread,
Oct 21, 2009, 7:42:52 AM10/21/09
to
Am Wed, 14 Oct 2009 16:57:58 -0400 schrieb Steve C:

> sub _setGDImage {
> my ($self, $image) = @_;
> return $self->Photo(-data => encode_base64($image->png()), -format =>
> 'png');
> }

To quote from the man-page of Tk::Photo:
"-data => string
Specifies the contents of the image as a string. The string can
contain base64 encoded data or binary data. The format of the
string must be one of those for which there is an image file format
handler that will accept string data."

So, it _should_ be possible to supply binary data. But I don't have figured
out how. On the other hand the above code seems to suggest, that the man-page
is wrong...
At least the encode_base64() doesn't seem to hurt much.

Oliver

Josef Moellers

unread,
Oct 21, 2009, 9:47:31 AM10/21/09
to
Oliver 'ojo' Bedford wrote:
> Hi,
>
> I am supplying image data (BMP or PNG) via the -data option to Tk::Photo.
> Is it necessary for the data string to be BASE65-encoded?

No.

Here's a small script that I just wrote:

#! /usr/bin/perl

use warnings;
use strict;


use Tk;
use Tk::PNG;

die "usage: $0 png\n" if @ARGV != 1;
my $top = new MainWindow;
my $p = $top->Photo(-format => 'png', -file => $ARGV[0]);
my ($width, $height) = ($p->width, $p->height);
print STDERR "width=$width, height=$height\n";
my $c = $top->Canvas(-width => $width, -height => $height)->pack();
$c->createImage(0, 0, -image => $p, -anchor => 'nw');
$c->bind('all', '<ButtonRelease-1>' => \&show);

MainLoop;

sub show {
my $wid = shift;

my $e = $wid->XEvent;
print $e->x, ', ', $e->y, "\n";
}

--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html

Oliver 'ojo' Bedford

unread,
Oct 21, 2009, 1:15:36 PM10/21/09
to
Am Wed, 21 Oct 2009 15:47:31 +0200 schrieb Josef Moellers:

> Oliver 'ojo' Bedford wrote:
>> Hi,
>>
>> I am supplying image data (BMP or PNG) via the -data option to
>> Tk::Photo.
>> Is it necessary for the data string to be BASE65-encoded?
>
> No.
>
> Here's a small script that I just wrote:

> [...]

> my $p = $top->Photo(-format => 'png', -file => $ARGV[0]);

[...]

Josef, you are using the -file option, whereas I was referring to -data.
Point is, I want to avoid file I/O in order to save time. So I'm
manipulating an image in memory and supply it via -data to Tk::Photo.

Oliver

Josef Moellers

unread,
Oct 22, 2009, 4:18:21 AM10/22/09
to

Oops. Sorry.

I just tried and apparently the -data requires the data to be base64
encoded, despite the manpage saying:

"The string can contain base64 encoded data or binary data.".

I tried by reading the entire image into a string and then calling
encode64() from the MIME::Base64 module.
It failed without the encoding:

couldn't recognize image data at /usr/lib/perl5/Tk/Image.pm line 21.

and worked with the encoding.

Josef

0 new messages