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

Hi everyone I would like to replace the following line:
$canvas->Photo(-file => "image.gif")
..with this one:
$canvas->Photo(-data => $imagedata)
I have read the other postings regarding this matter, but i still can't figure out how to read the image data from the .gif file and present it, correctly formatted, to the Tk::Photo function.

Please help me out on this one.

Regards Søren Schimkat

Replies are listed 'Best First'.
Re: TK::Photo -file -data
by clintp (Curate) on Apr 17, 2002 at 14:29 UTC
    If you use -data, I believe the -format option is required.
      Yes - that's correct. But my problem is how to read / parse the gif file and correctly format the imagedata for the -data option.

      Below is an example for doing the job using jpeg images and GD - but i need to use gif's (or perhaps png's) and I would like to awoid having to use GD when showing the images in the final script which should get it's parsed image data from a dbmopned hash. So the question is...

      Does anyone know how to parse the imagedata from a Gif?

      Thanx in advance, Søren Schimkat
      #!/usr/bin/perl use GD; use Tk; use Tk::JPEG; use MIME::Base64; my $im = GD::Image->new('image.jpg'); my $img = encode_base64($im->jpeg()); my $main = MainWindow->new(); my $image = $main->Photo("button", -data => $img, -format => 'jpeg'); my $label= $main->Label(-image => "button"); $label->grid(-in => $main); MainLoop;
        ... for visualizing what i meant with having the imagedata in a dbmopned hash, I have rewritten the example.

        First a script for creating the hash...
        #!/usr/bin/perl use GD; use Tk; use Tk::JPEG; use MIME::Base64; dbmopen %myhash, "myhash", 0666; my $im = GD::Image->new('image.jpg'); my $img = encode_base64($im->jpeg()); $myhash{image} = $img; dbmclose %myhash;


        ... and the a script for showing the image - using the imagedata in the hash:

        #!/usr/bin/perl use Tk; use Tk::JPEG; dbmopen %myhash, "myhash", 0666; my $main = MainWindow->new(); my $image = $main->Photo("button", -data => $myhash{image}, -format => + 'jpeg'); my $label= $main->Label(-image => "button"); $label->grid(-in => $main); dbmclose %myhash; MainLoop;


        One of the great things is the compression:
        My image.jpg is 14.9 Kb.
        When compressed it is 13.4 Kb.
        When compressing the hash containing the imagedata - it's only 7.7 Kb.

        That's cool.

        Søren Schimkat