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

i've read the Perl::Magick docs online *again* because I need to do something I haven't done before: reduce the color space programmatically.

the client is uploading a handful ( 1 .. n ) PNG files. i can Get the colors using $obj->Get( 'colors' ), but in the documentation there's doesn't seem to be an analog for setting the same parameter ...

the online docs indicate that options that are "gettable" have analogs in convert ... but attempting to set the colors gives error 410, which is:

410 OptionError a command-line option was malformed
the code i've tried:
my $err = $magick->Resize( height => 338, width => 450, colors => 128 +);
and
my $err = $magick->Resize( height => 338, width => 450 ); $err = $magick->Set( -colors => 128 );
both of which return the 410 error ...

can the number of colors be reset?

Replies are listed 'Best First'.
Re: Image::Magick and colors
by kvale (Monsignor) on Jul 18, 2005 at 18:49 UTC
    Reducing the colorspace is a nontrivial operation that involves approximating a large set of colors with a smaller set, so it makes sense that you cannot just set number of colors. To reduce the size of the color space, check out the Posterize and Quantize methods in Image::Magick.

    -Mark

      Quantize *seems* to be what i needed ...

      i'll just need to see how ugly the end result becomes, and tweak the parameters appropriately

Re: Image::Magick and colors
by japhy (Canon) on Jul 18, 2005 at 18:42 UTC
    On this page of the docs, I don't see anything listed that seems to help you. But when I look at the convert docs, I get to the following page which defines their color-reduction algorithm.

    Perhaps what you have to do is go through the color indices yourself, and copy certain values over others, thus reducing the number of unique colors in the image.


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      you're correct. there's no immediately analogous Settable attribute listed there, but further down on the page ( underneath the table of Gettable attribs ) there is this little note:
      GetAttribute() is an alias for method Get().

      Most of the attributes listed above have an analog in convert. See the documentation for a more detailed description of these attributes.

      which leads me to believe there *is* a way ... but convert is the command-line util ...

      i'm *not* relishing reducing the color depth by dealing with the color indices ...

        I'd say you should try contacting the maintainers of ImageMagick (not the Perl module) and find out if they'll divulge the secret of convert's color-reduction.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart