Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > ImageMagick: Writing image with 4 colors and palette

Reply
Thread Tools

ImageMagick: Writing image with 4 colors and palette

 
 
Josef Moellers
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      11-03-2008
Josef Moellers <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      11-03-2008
Joost Diepenmaat wrote:
> Josef Moellers <(E-Mail Removed)> 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
 
Reply With Quote
 
zentara
Guest
Posts: n/a
 
      11-03-2008
On Mon, 03 Nov 2008 10:20:30 +0100, Josef Moellers
<(E-Mail Removed)> 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

 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      11-03-2008
zentara wrote:
> On Mon, 03 Nov 2008 10:20:30 +0100, Josef Moellers
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      11-03-2008
Josef Moellers <(E-Mail Removed)> 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/
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      11-03-2008
Joost Diepenmaat wrote:
> Josef Moellers <(E-Mail Removed)> 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
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      11-03-2008
Joost Diepenmaat wrote:
> Josef Moellers <(E-Mail Removed)> 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
 
Reply With Quote
 
zentara
Guest
Posts: n/a
 
      11-04-2008
On Mon, 03 Nov 2008 16:12:55 +0100, Josef Moellers
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeNode colors come from anchor colors AAaron123 ASP .Net 1 08-07-2008 07:56 PM
Manipulating GIF image frames w/ PIL - where'd my palette go? skip@pobox.com Python 0 09-04-2006 04:07 PM
Image magick palette kkarma Perl Misc 0 07-31-2004 03:16 AM
Q: How change color palette of Image in J2ME? Kenneth Ljunggren Java 0 06-16-2004 06:39 AM
Displaying Palette Tiff Image using JAI (very slow when scale operation is performed) atusc Java 1 06-04-2004 05:24 AM



Advertisments