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

When I try to display a gif image with Tk, some of the top portion of the image displays, but the rest is black, some example of my code would look like this:
$mw->Photo('image1',-file => 'image1.gif'); $mw->Label(-image => 'image1')->pack();
I am running activeperl on windows XP, the Tk version is 8. This doesnt happen with all images, just most of them. Any ideas why this happens? Thanks!

Edit by castaway - added code tags

Replies are listed 'Best First'.
Re: when i display a gif image in tk, the bottom half is black, why?
by eserte (Deacon) on May 10, 2004 at 09:00 UTC
    There was a bug in certain Tk versions (I think 800.024 was among them) which caused not calling "binmode" internally on image files.

    You may workaround the problem either by switching to a newer Perl/Tk version (I think both Tk800.025 and Tk804.027 should do), or use the -data option instead of the -file option to load the file. From memory, untested:

    use MIME::Base64; { local $/ = undef; open(my $fh, "image1.gif") or die; binmode $fh; $buf = <$fh>; } $mw->Photo("image1", -data => encode_base64($buf));
      surprisingly, current perl binary for perl-5.8.3 provided by ActiveState (build 809) have Tk800.024 with this problem not solved.

      Namely, "widget" demo suffers from this at item #7 and #8

      I wonder why they didn't included 800.025, which was available at a time of build...

      Courage, the Cowardly Dog

Re: when i display a gif image in tk, the bottom half is black, why?
by graff (Chancellor) on May 10, 2004 at 02:43 UTC
    You might want to find the smallest gif file that demonstrates this problem and forward it to the module author along with the minimal complete script that displays the file. My first guess would be that because you're on a Windows system, the module might not be doing "binmode" on the input file, and whenever the gif file happens to contain a "^Z" byte (0x1a, the text-mode "end-of-file" marker), the input terminates at that point.

    Actually, I would be very surprised if this turns out to be the problem -- the binmode/textmode problem is as old as MS-DOS, and Perl/Tk has been around on Windows long enough that this should have been resolved by now.

Re: when i display a gif image in tk, the bottom half is black, why?
by marcz908 (Novice) on May 10, 2004 at 14:48 UTC
    Hey! this was actually my post, I forgot to log myself in. Well problem solved! 800.024 is the version that I have, I just used the code provided in the reply and everything works now, looks like I need to upgrade. Thanks again!