ImageMagick: Writing image with 4 colors and palette

Discussion in 'Perl Misc' started by Josef Moellers, Nov 3, 2008.

  1. I'd like to create a set of images that are subsequently used to build
    the menu part of a DVD. I may have to fine-tune colors and positions,
    I'd like to create them using a program where I can simply tweak some
    parameters rather than use an image manipulation program like gimp to
    draw the image(s), as I need 4 images: one that will later form the
    "background" and three that will be used to display the buttons. The
    latter have to have just 4 colors and have to have a palette.

    I've tried all sorts of combinations of
    $img->Set(type => 'Palette');
    $img->Quantize(colors => 4);
    but still, if I open the image in gimp, it still shows as RGB rather
    than indexed.

    How do I make sure that an image I created has only 4 colors and a palette?

    Thanks,

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 3, 2008
    #1
    1. Advertising

  2. Josef Moellers <> writes:

    > I'd like to create a set of images that are subsequently used to build
    > the menu part of a DVD. I may have to fine-tune colors and positions,
    > I'd like to create them using a program where I can simply tweak some
    > parameters rather than use an image manipulation program like gimp to
    > draw the image(s), as I need 4 images: one that will later form the
    > "background" and three that will be used to display the buttons. The
    > latter have to have just 4 colors and have to have a palette.
    >
    > I've tried all sorts of combinations of
    > $img->Set(type => 'Palette');
    > $img->Quantize(colors => 4);
    > but still, if I open the image in gimp, it still shows as RGB rather
    > than indexed.
    >
    > How do I make sure that an image I created has only 4 colors and a palette?


    I don't use the module (I prefer to use the command line versions of
    image magick) but you probably want the -colors option (possibly with
    the "-type palette" option) make /make sure you save to a file format
    that supports palettes, like png/

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Nov 3, 2008
    #2
    1. Advertising

  3. Joost Diepenmaat wrote:
    > Josef Moellers <> writes:
    >
    >> I'd like to create a set of images that are subsequently used to build
    >> the menu part of a DVD. I may have to fine-tune colors and positions,
    >> I'd like to create them using a program where I can simply tweak some
    >> parameters rather than use an image manipulation program like gimp to
    >> draw the image(s), as I need 4 images: one that will later form the
    >> "background" and three that will be used to display the buttons. The
    >> latter have to have just 4 colors and have to have a palette.
    >>
    >> I've tried all sorts of combinations of
    >> $img->Set(type => 'Palette');
    >> $img->Quantize(colors => 4);
    >> but still, if I open the image in gimp, it still shows as RGB rather
    >> than indexed.
    >>
    >> How do I make sure that an image I created has only 4 colors and a palette?

    >
    > I don't use the module (I prefer to use the command line versions of
    > image magick) but you probably want the -colors option (possibly with
    > the "-type palette" option) make /make sure you save to a file format
    > that supports palettes, like png/


    Bedankt, but I've tried that (see above). And ... yes, the output is
    going to be PNG, as spumux wants it that way. The main image will be
    converted to jpeg, and made into a short movie.

    I can't use the command line version of ImageMagick as I need to have
    information like the bounding boxes of text items which I want to put
    into an XML file for spumux.

    Groetjes,

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 3, 2008
    #3
  4. Josef Moellers

    zentara Guest

    On Mon, 03 Nov 2008 10:20:30 +0100, Josef Moellers
    <> wrote:

    >I'd like to create a set of images that are subsequently used to build
    >the menu part of a DVD. I may have to fine-tune colors and positions,
    >I'd like to create them using a program where I can simply tweak some
    >parameters rather than use an image manipulation program like gimp to
    >draw the image(s), as I need 4 images: one that will later form the
    >"background" and three that will be used to display the buttons. The
    >latter have to have just 4 colors and have to have a palette.
    >
    >I've tried all sorts of combinations of
    >$img->Set(type => 'Palette');
    >$img->Quantize(colors => 4);
    >but still, if I open the image in gimp, it still shows as RGB rather
    >than indexed.
    >
    >How do I make sure that an image I created has only 4 colors and a palette?
    >
    >Thanks,
    >
    >Josef


    I believe you need to save the image as png to get a palette.
    If you save as jpg, it won't work.

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Image::Magick;

    my $file = shift or die "Need a file $!\n";

    my $img = Image::Magick->new;
    $img->ReadImage($file);

    $img->Quantize( colors => 4 );
    $img->Set( type => 'Palette' );
    $img->Write("$0.png");
    __END__

    zentara
    zentara, Nov 3, 2008
    #4
  5. zentara wrote:
    > On Mon, 03 Nov 2008 10:20:30 +0100, Josef Moellers
    > <> wrote:
    >
    >> I'd like to create a set of images that are subsequently used to build
    >> the menu part of a DVD. I may have to fine-tune colors and positions,
    >> I'd like to create them using a program where I can simply tweak some
    >> parameters rather than use an image manipulation program like gimp to
    >> draw the image(s), as I need 4 images: one that will later form the
    >> "background" and three that will be used to display the buttons. The
    >> latter have to have just 4 colors and have to have a palette.
    >>
    >> I've tried all sorts of combinations of
    >> $img->Set(type => 'Palette');
    >> $img->Quantize(colors => 4);
    >> but still, if I open the image in gimp, it still shows as RGB rather
    >> than indexed.
    >>
    >> How do I make sure that an image I created has only 4 colors and a palette?
    >>
    >> Thanks,
    >>
    >> Josef

    >
    > I believe you need to save the image as png to get a palette.
    > If you save as jpg, it won't work.
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    > use Image::Magick;
    >
    > my $file = shift or die "Need a file $!\n";
    >
    > my $img = Image::Magick->new;
    > $img->ReadImage($file);
    >
    > $img->Quantize( colors => 4 );
    > $img->Set( type => 'Palette' );
    > $img->Write("$0.png");
    > __END__


    BTDTNT :-(
    See my codelines: I did try the colors and the type stuff to no avail.
    I also tried your code on the output of my program, but "file" still says
    PNG image data, 720 x 576, 16-bit/color RGBA, non-interlaced
    and GIMP still has the indicator on Image -> Mode -> RGB.

    I tried it the other way round: made it Indexed with 4 colors using
    GIMP, then used ImageMagick's "Get()" function to see what type and
    colors it had, but, not surprisinly, it said "PaletteMatte" (tried that,
    too) and colors=2.

    BTW I'm using "ImageMagick 6.2.4 10/02/07 Q16".

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 3, 2008
    #5
  6. zentara wrote:

    > I believe you need to save the image as png to get a palette.
    > If you save as jpg, it won't work.


    Sorry, forgot: Yes, I do save them as PNGs, the conversion to jpeg is
    done later.

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 3, 2008
    #6
  7. Josef Moellers <> writes:

    > zentara wrote:


    > See my codelines: I did try the colors and the type stuff to no avail.
    > I also tried your code on the output of my program, but "file" still says
    > PNG image data, 720 x 576, 16-bit/color RGBA, non-interlaced
    > and GIMP still has the indicator on Image -> Mode -> RGB.
    >
    > I tried it the other way round: made it Indexed with 4 colors using
    > GIMP, then used ImageMagick's "Get()" function to see what type and
    > colors it had, but, not surprisinly, it said "PaletteMatte" (tried
    > that, too) and colors=2.
    >
    > BTW I'm using "ImageMagick 6.2.4 10/02/07 Q16".
    >
    > Josef


    I just tried that code:

    my $file = shift or die "Need a file $!\n";

    my $img = Image::Magick->new;
    $img->ReadImage($file);

    $img->Quantize( colors => 4 );
    $img->Set( type => 'Palette' );
    $img->Write("test.png");


    and the output of "identify test.png" is:

    test.png PNG 1600x1200 1600x1200+0+0 8-bit PseudoClass 4c 259kb

    note the 4c, indicating a 4 color palette. gimp opens it in indexed mode
    too.

    That's with Image::Magick 6.4.3 - you may want to try upgrading.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Nov 3, 2008
    #7
  8. Joost Diepenmaat wrote:
    > Josef Moellers <> writes:


    >> BTW I'm using "ImageMagick 6.2.4 10/02/07 Q16".


    > That's with Image::Magick 6.4.3 - you may want to try upgrading.


    I was alrady afraid, that that would be the reason.

    Thanks to both, you and Zentara,

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 3, 2008
    #8
  9. Joost Diepenmaat wrote:
    > Josef Moellers <> writes:
    >
    >> zentara wrote:

    >
    >> See my codelines: I did try the colors and the type stuff to no avail.
    >> I also tried your code on the output of my program, but "file" still says
    >> PNG image data, 720 x 576, 16-bit/color RGBA, non-interlaced
    >> and GIMP still has the indicator on Image -> Mode -> RGB.
    >>
    >> I tried it the other way round: made it Indexed with 4 colors using
    >> GIMP, then used ImageMagick's "Get()" function to see what type and
    >> colors it had, but, not surprisinly, it said "PaletteMatte" (tried
    >> that, too) and colors=2.
    >>
    >> BTW I'm using "ImageMagick 6.2.4 10/02/07 Q16".
    >>
    >> Josef

    >
    > I just tried that code:
    >
    > my $file = shift or die "Need a file $!\n";
    >
    > my $img = Image::Magick->new;
    > $img->ReadImage($file);
    >
    > $img->Quantize( colors => 4 );
    > $img->Set( type => 'Palette' );
    > $img->Write("test.png");
    >
    >
    > and the output of "identify test.png" is:
    >
    > test.png PNG 1600x1200 1600x1200+0+0 8-bit PseudoClass 4c 259kb
    >
    > note the 4c, indicating a 4 color palette. gimp opens it in indexed mode
    > too.
    >
    > That's with Image::Magick 6.4.3 - you may want to try upgrading.


    This is getting strange:

    I upgraded to 6.4.5, thenI took your program, added the necessary
    decorations and
    print $img->Get('version'), "\n";

    josef@bounty:~/Perl$ ./Joost xB.png
    ImageMagick 6.4.5 2008-11-03 Q16 http://www.imagemagick.org
    josef@bounty:~/Perl$ identify test.png
    test.png PNG 720x576+0+0 DirectClass 8-bit 5.2k 0.040u 0:01

    It seems that it has to do with the fact that I have a transparent
    background ...

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 3, 2008
    #9
  10. Josef Moellers

    zentara Guest

    On Mon, 03 Nov 2008 16:12:55 +0100, Josef Moellers
    <> wrote:


    >It seems that it has to do with the fact that I have a transparent
    >background ...


    Guess to remove layer:
    Try adding the flatten option.

    zentara
    zentara, Nov 4, 2008
    #10
  11. zentara wrote:
    > On Mon, 03 Nov 2008 16:12:55 +0100, Josef Moellers
    > <> wrote:
    >
    >
    >> It seems that it has to do with the fact that I have a transparent
    >> background ...

    >
    > Guess to remove layer:
    > Try adding the flatten option.


    I've tried to call $img->Flatten() and $img->Layers(method =>
    'flatten'), none of which worked.

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 4, 2008
    #11
  12. Josef Moellers

    zentara Guest

    On Tue, 04 Nov 2008 13:53:57 +0100, Josef Moellers
    <> wrote:

    >zentara wrote:
    >> On Mon, 03 Nov 2008 16:12:55 +0100, Josef Moellers
    >> <> wrote:
    >>
    >>
    >>> It seems that it has to do with the fact that I have a transparent
    >>> background ...

    >>
    >> Guess to remove layer:
    >> Try adding the flatten option.

    >
    >I've tried to call $img->Flatten() and $img->Layers(method =>
    >'flatten'), none of which worked.
    >
    >Josef


    This worked for me:
    tig.png is a green curve on a transparent alpha layer

    tig.png PNG 32x32 32x32+0+0 8-bit DirectClass 5.4e+02b
    #after script is run
    tig-4c.png PNG 32x32 32x32+0+0 8-bit PseudoClass 4c 3e+02b

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Image::Magick;
    use MIME::Base64;

    # tig.png is a green curve written on a transparent layer
    # included here so we are on same page
    my $blob = decode_base64(
    'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBI
    WXMAAAsTAAALEwEAmpwYAAABwUlEQVRYw+2VvS8EURTFf8/4CKJQTKOgEIlEqFCIRnSUhE6iUOhV
    Go3Kf0ChkiipNWqFQuErEtFJSEREfO7O0dyNid2Z3X3sbmHfy82bdzPv3TPnnnvHSaKWo4EajzqA
    OoBGn0MOl+cT+l8pmANugEdb53wBuHIbkdGfAYKYOws0+qTBl4GgyL5ehhUHcFVkX3ERfm9jraAq
    IrQgLmbewX+TghcgsvKLAFVbA895xFQLgMOFQHstq2ACaMnD5TsklWyIEPGMiFDe3C7nrpyVy8AI
    0JaQ81mHO3C4oJIp2E0R3TswBKxWJAWIW6M+W4D+LOLd7BaxWPq9xQP3I64teITIxAIvIc7t+dPW
    N8QdIvwrAKex4PG5guhAjMd8T8bCh4m13wuAqX0H8ZCg+GlEA6IV0YZYLvBOZKycIcaSADhJuebS
    B0wCvcAUECbIZgC4AJqAZhNfaNWxA4wWOBMBl8AecAIc2n/lHsQ8Yh9xYvQlzW3EIKIZ0fKDsU7z
    dyO2TANKYCWyFB0jZkgJmJsZxIZRHqRoBYQzG0AclXD3axqAF8SaT3czED2ILsRmKoQCrjvEQtrX
    egAKEMOI9R9lHH0BkYPO/OE8jeIAAAAASUVORK5CYII=');

    my $img = Image::Magick->new(magick=>'png');
    $img->BlobToImage( $blob );
    $img->Write('tig.png');

    $img->Quantize( colors => 4 );
    $img->Set( type => 'Palette' );
    $img->Write("tig-4c.png");
    __END__

    I'm using IM-6.4.3

    Maybe your libpng is defective?

    zentara
    zentara, Nov 5, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. atusc
    Replies:
    1
    Views:
    4,189
  2. Kenneth Ljunggren

    Q: How change color palette of Image in J2ME?

    Kenneth Ljunggren, Jun 16, 2004, in forum: Java
    Replies:
    0
    Views:
    997
    Kenneth Ljunggren
    Jun 16, 2004
  3. Replies:
    0
    Views:
    396
  4. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    528
    darrel
    Aug 7, 2008
  5. ZelluX
    Replies:
    3
    Views:
    1,859
    Peter Otten
    Dec 1, 2008
Loading...

Share This Page