how to tell if image has 8 bit indexed palette

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

how to tell if image has 8 bit indexed palette

Post by jmaeding »

I am making tools that do conversions and make decisions based on if an image fits these categories:
1 bit black and white
8 bit greyscale
8 bit RGB indexed palette
24 bit RGB

everything else above that is considered 24 bit RGB to me, since we just handle that as a "full color" image, as that fills my needs for high quality color images.
It seems I can figure out what category an image is in, using idetify and:
-format %[colorspace],%z,%k
If colorspace is gray, check the bit depth (%z) for 1 or 8 to see if BW or grayscale.
If colorspace is RGB, check unique colors (%k) to see if its below 256, that would be the indexed color image.

Problem is the %k (unique colors) is slow as IM has to look through the whole image. -ping does not help, and I am dealing with images 10k x 10k pixels.
It just seems like the wrong way to do things too - checking the unique colors - not bulletproof by any means.

Also, I am testing IM through a DOS prompt in windows 7 64 bit. Would running it through the com wrapper be faster?
My final tools are done in C#, so I could use the com or .net wrappers.
thanks
James Maeding
Civil Engineer / Programmer
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to tell if image has 8 bit indexed palette

Post by anthony »

Most of those categories are basically based on number of colors in an image

Code: Select all

    identify -format %k  image 
For example

Code: Select all

identify -format %k rose:
3019
Clearly shows that it is not a 8 bit pallete image.


However there is also a matter of 'near' catagories. For example a line drawing may be grayscale, but really it is more black and white, and then mostly white (typically).

This categorization is looked at in some detail in IM Examples, Image Comparison.
Sorting Images by Type
http://www.imagemagick.org/Usage/compare/#type_general
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: how to tell if image has 8 bit indexed palette

Post by jmaeding »

ah, did not see that section before, will read.
The problem (possibly unavoidable) id the %k takes a long time on big images.
I am seeing 5 to 10 second second return times.
At least it handle the large images, not like my previous GDI routines in C#.
James Maeding
Civil Engineer / Programmer
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: how to tell if image has 8 bit indexed palette

Post by jmaeding »

I read the suggested usage section, but those examples do not address the issue of if an image has an indexed color palette, or RGB.
Also, they use the -verbose option of idetify, which locks up on most large images I have.
I was impressed at the cleverness of how to categorize images, its just that those are relatively small images.

Isn't there some flag that tells if each pixel has RGB value, or if it uses a palette?
That seems so fundamental to image manipulation.
Even if I do have to use -verbose, which item nails it down?
thanks
James Maeding
Civil Engineer / Programmer
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to tell if image has 8 bit indexed palette

Post by fmw42 »

Here I create a palette image:

convert rose: +dither -colors 256 -type palette rose_palette.gif

Now I check for its type and colorspace

convert rose_palette.gif -format %r info:
PseudoClassRGB

The pseudoclass type means palette (8-bit)

See string formats at http://www.imagemagick.org/script/escape.php

whereas

convert rose: -format %r info:
DirectClassRGB


Where DirectClass means 8-bit or 16-bit per channel color (depending upon your IM Quantumlevel at compile Q8 or Q16 or if you have added say -depth 8 to the command.
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: how to tell if image has 8 bit indexed palette

Post by jmaeding »

ah! thanks a bunch for clarifying that.
Hope I can contribute back sometime as I use IM more and more in Engineering.
James Maeding
Civil Engineer / Programmer
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to tell if image has 8 bit indexed palette

Post by anthony »

jmaeding wrote:I was impressed at the cleverness of how to categorize images, its just that those are relatively small images.
The reason for that is simple. I typically do not compare large images. At least not initially.
Instead I generate small thumbnails and even smaller 'metrics' which are then used to categorize the images. The key is to avoid processing very large images, except as a final step.

It can take some time to generate the small images and metrics, but it only needs to be done once per image (linear time), and then all processing is with the smaller image/metric.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: how to tell if image has 8 bit indexed palette

Post by jmaeding »

That pre thumbnailing makes sense and is similar to how I handle tracking large amounts of autocad drawings.

I tried the %r on a png which is 8 bit indexed RGB color, and it gave back DirectClassRGBMatte.
I double checked the format of the image with Corel PhotoPaint, and it was still 8 bit indexed.

Is there a place I can read about the image class that explains the return types?
I guess i could run a bunch of tests and compare to try to infer the meanings, but I would miss certain things for sure.
thx
James Maeding
Civil Engineer / Programmer
jmaeding
Posts: 54
Joined: 2006-05-03T09:48:26-07:00

Re: how to tell if image has 8 bit indexed palette

Post by jmaeding »

I did more searching, and found some web pages mentioning how to detect a mask in a tiff.
The mask would be listed as <something>Matte
I reopened my test image in corel PP that was indexed, and noticed it had a selection border around it.
I flattened it and then got the pseudoclass result from IM.

I had no idea a png could exist as a mask somehow. I need to read on that more.
I need to have IM remove the mask when i run into this so I'l look into that also.
I'd still like to know if there is more documentation on the class type.
James Maeding
Civil Engineer / Programmer
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to tell if image has 8 bit indexed palette

Post by fmw42 »

see http://www.imagemagick.org/Usage/formats/#png

But I am not sure that the bug with the palette PNG with transparency has been fixed in IM, yet. IM's handling of PNG is slowly being improved by glennrp.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to tell if image has 8 bit indexed palette

Post by anthony »

Did you use the %r with "convert" or with "identify" ?

Many image processing operations in "convert" cold remove a no longer relevant palette, and that may interfere.

Prehaps a small example image and the command you tried, so we can have a go as well.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply