in reply to Re: Re: TK::Photo -file -data
in thread TK::Photo -file -data

... 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

Replies are listed 'Best First'.
Re: Re: Re: Re: TK::Photo -file -data
by schimkat (Initiate) on Apr 18, 2002 at 14:23 UTC
    Now it works with GIF's. :-)
    Just use GD ver. 1.19 instead of 1.38 which is the current release.

    Script for creating hash:
    #!/usr/bin/perl use GD; use Tk; use MIME::Base64; dbmopen %myhash, "myhash", 0666; open (GIF,"image.gif"); $im = newFromGif GD::Image(GIF); close GIF; my $img = encode_base64($im->gif); $myhash{image} = $img; dbmclose %myhash;

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


    Regards Søren Schimkat
      ... and finally... a real life script: CatanAid

      I have dropped the hash and replaced it with a plain text file for better compatibillity with different platforms.

      Regards Søren Schimkat
        I´ve been asked how I create the datafile for CatanAid - so here is the code I used for CatanAid v. 0.1 (current version):
        #!/usr/bin/perl use GD; use Tk; use MIME::Base64; open (DATA, ">graphicsdata"); foreach (<images/*>) { my $type = (split/\//, $_)[1]; foreach (<$_/*.gif>) { my $name = (split /\./,((split/\//, $_)[2]))[0]; print "Adding imagedata to datafile: $type/$name ($_).\n"; open (GIF, $_); $data = encode_base64((newFromGif GD::Image(GIF))->gif); close GIF; print DATA "<" . $type . "/" . $name . ">\n" . $data; } } close DATA;
        The images are regular .gif images placed in a simple directory structure:
        images/interface/expansionbutton.gif images/interface/background.gif images/interface/standardbutton.gif images/landscapes/mountain.gif images/landscapes/dessert.gif images/landscapes/forest.gif images/landscapes/hill.gif images/landscapes/pasture.gif images/landscapes/plain.gif images/landscapes/sea.gif images/ports/mountain.gif images/ports/forest.gif images/ports/hill.gif images/ports/surprise.gif images/ports/pasture.gif images/ports/plain.gif
        Well... this should put at lid on this discussion i´ve been having with myself. :-)

        Regards Søren Schimkat