Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

too many colors, using Tk to write images

by elbie (Deacon)
on Jun 11, 2003 at 19:33 UTC ( #265149=perlquestion: print w/ replies, xml ) Need Help??
elbie has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use Tk to superimpose two images, then write them back out again, but I'm getting an error "too many colors at /Library/Perl/darwin/Tk.pm line 228."

Here's a scaled down script that reproduces the error:

#! /usr/bin/perl -w use strict; use Tk; my( $background, $score_pip ); # Main { my $top = MainWindow->new(); $background = $top->Photo( -file => "QBackground.gif", # -palette => 256/256/256, ); $score_pip = $top->Photo( -file => "Score.gif", # -palette => 256/256/256, ); $background->copy( $score_pip, -to => ( 180 - 16, 180 - 16 )); # $background->redither; $background->write( 'testout.gif', -format => 'gif', # -grayscale, ); }

I tried playing with the palette options, and the redither method (commented out above) to see if they would help, but they didn't.

The greyscale option helped, of course, but the image was in greyscale. Not so good.

Also, is there a way to generate a Photo object without creating it using a parent object? I tried $foo = Tk::Photo( ... ) and similar, but those didn't seem to work.

elbieelbieelbie

Comment on too many colors, using Tk to write images
Download Code
Re: too many colors, using Tk to write images
by bbfu (Curate) on Jun 11, 2003 at 21:26 UTC

    I'm fairly certain there's no way to create a Tk::Photo without a parent widget, as it assumes a certain amount of context that's provided by the parent.

    My guess regarding running out of colors is that the palettes of the GIF files contain slightly different colors, so they can't be combined naively. I don't know which line of the code you posted is line 228 and generating the error, but if it's the copy line, then the redither won't help because the program never gets to that point. Update: The redither won't help regardless, as it doesn't actually reduce the colors used by the image. If I understand the docs correctly, it's for display purposes only.

    If it's the write line, then it may be that the combined palettes contain too many colors to be supported by GIF (IIRC, GIF only supports up to 256 colors per image block; though you can do some trickery using multiple image blocks to get more than 256 colors total in the image, I'd be willing to bet Tk doesn't support that).

    The best bet I can think of (and, of course, that doesn't mean the best bet in general ;) would be to reduce the number of palette entries used in the original images.

    In the end, however, I think you'd be better served by looking into the other image processing modules available, such as Image::Magick or GD, as their more sophisticated features would probably make this job easier.

    Good luck.

    Update: Oh, and I assume it's just a typo, but you would need quotes around the value you're passing to the palette option for the code to compile.

    bbfu
    Black flowers blossom
    Fearless on my breath

      The line 228 is in the Tk module, but yeah, it's being called through the write method. Commenting that line out eliminates the error.

      Assuming that the 256 colour limit is specific to the gif is a good guess. I can play with the palette values to see if that does it. I assume it's a gif thing, as just displaying the image (part of the original code I derived the example from) seems to work fine.

      The quotes don't seem to matter, btw. It seems to do what I want, which I think is a result of specifying the options using the hash-like -var => value method of passing parameters.

      And yeah, I'm investigating other tools. I'd forgotten about GD though. Funny that. Thanks!

      elbieelbieelbie

        The line 228 is in the Tk module, but yeah, it's being called through the write method. Commenting that line out eliminates the error. Assuming that the 256 colour limit is specific to the gif is a good guess. I can play with the palette values to see if that does it. I assume it's a gif thing, as just displaying the image (part of the original code I derived the example from) seems to work fine.

        Then it sounds like it's definitely an issue GIF format. You need to find a way to reduce the palette down to 256 colors. Unfortunately, I didn't see any options in the Tk::Image (er, I mean Tk::Photo, and I guess you could always try calling $photo->configure(-palette => '256/256/256');) docs to do so. I'll look some more, though.

        The quotes don't seem to matter, btw. It seems to do what I want, which I think is a result of specifying the options using the hash-like -var => value method of passing parameters.

        Well, only the left side of the => operator is special. Perl would actually parse -palette => 256/256/256, as: -palette => 0.00390625, (ie, 1 divided by 256). I really don't see how that would get you anything but a monochrome image but I guess I'll take your word for it. :)

        Anyway, let us know how it all turns out for you. Good luck.

        bbfu
        Black flowers blossom
        Fearless on my breath

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://265149]
Approved by Thelonius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-09-21 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls