Page 1 of 1

Extracting colormap and gray/alpha information

Posted: 2013-03-07T08:22:34-07:00
by timholy
I'm using ImageMagick to import image data into Julia (a new programming language). I easily got the basics working, but I'm having some trouble with the more subtle issues:
  • For indexed images (i.e., images with a colormap), I'd like to separately extract both the index and the colormap. I think I've gotten the index out,

    Code: Select all

    convert $filename -channel Index -separate -depth $bitdepth gray:-
    but I've not been able to find anything online about the colormap.
  • I know how to get out data in one "go" as an array, using lines such as

    Code: Select all

    convert $filename -depth $bitdepth $colorspace:-
    This works fine for colorspace = "rgba" or colorspace = "gray". But what about images with grayscale and an alpha channel? I can extract them separately and then paste them together, but if gray and alpha are stored sequentially for each pixel on disk (I don't know if that's the case), it seems better to get both at once.
I asked the first of these questions elsewhere (stackoverflow), but perhaps this is the better site.

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-07T09:51:15-07:00
by snibgo
For question 2: rgba also works for greyscale images with alpha. You'll get the same values for R, G and B.

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-07T10:07:54-07:00
by timholy
I'd rather just get the gray values if it's a gray image.

But question 2 is not nearly as important as question 1; I can certainly live with two calls to convert to extract gray and alpha separately.

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-07T10:24:42-07:00
by snibgo
For Q1, I didn't know we could do "-channel Index", and I don't know the difference between an index and colormap. Does "identify -verbose" give you what you need?

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-07T10:30:34-07:00
by fmw42
I am confused about what you mean by index and colormap. The command
convert $filename -channel Index -separate -depth $bitdepth gray:-
does not appear to me to give anything but the image pixel-by-pixel values. There is no -channel option of Index to my knowledge according to the docs. I could be wrong and it is just not listed.

If you want just the grayscale image, then

convert yourimage -colorspace gray -depth ... gray:-

or depending upon your IM version

convert yourimage -set colorspace RGB -colorspace gray -depth ... gray:-

If you want that as text, you can substitute txt:- for gray:-

The colormap to my understanding is can be found from the verbose info of the image.

identify -verbose yourimage


Edit: Sorry snibgo, you posted while I was composing my answer (both of us with the same ideas)

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-07T11:37:31-07:00
by snibgo
We do often cross-post, and even (shock!) often agree.

I don't recall seeing "-channel index" documented, but it is listed in "convert -list channel".

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-07T14:13:45-07:00
by fmw42
snibgo wrote:We do often cross-post, and even (shock!) often agree.

I don't recall seeing "-channel index" documented, but it is listed in "convert -list channel".

Perhaps a new feature that did not get into the options page documentation.

It is odd to me that I get the same results from:

convert rose: -channel index -separate txt:

and

convert rose: txt:

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-08T03:19:03-07:00
by timholy
To clarify, index/colormap refer to the following: let's say I have an RGB image, of size 800x600, that I've created with a drawing program. Because my drawing is not a full-realism painting, it actually only has 5 distinct colors in it. Rather than storing the RGB information for each pixel, I could store it as a lookup table:

index: a 800x600 array, each entry containing a value from 1 to 5 (or 0 to 4 if you're using 0-based indexing)
colormap: a 3x5 array, specifying the RGB color for each of those 5 index values

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-08T03:20:36-07:00
by timholy
...and yes, identify -verbose makes it clear that these are stored in the image separately. I just don't know how you ask for them directly.

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-08T06:34:52-07:00
by snibgo
Okay, I understand.

I played with an indexed (aka palette) image, and "-channel Index" didn't seem to give me the index value. I don't know how IM can provide these.

You can get the colormap from "identify -verbose", or "convert ... -unique-colors".

Re: Extracting colormap and gray/alpha information

Posted: 2013-03-10T04:03:28-07:00
by timholy
Hmm, I realized there's another issue with the second question: if one wants to write an image that has both a gray channel and an alpha channel, that would seem to require two calls to convert. But the second call will overwrite the file produced by the first one, correct? Or is there a way to avoid that?