ImageMagick: Writing image with 4 colors and palette

J

Josef Moellers

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
 
J

Joost Diepenmaat

Josef Moellers said:
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/
 
J

Josef Moellers

Joost said:
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
 
Z

zentara

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
 
J

Josef Moellers

zentara said:
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
 
J

Josef Moellers

zentara said:
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.
 
J

Joost Diepenmaat

Josef Moellers said:
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.
 
J

Josef Moellers

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
 
J

Josef Moellers

Joost said:
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
 
J

Josef Moellers

zentara said:
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
 
Z

zentara

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top