Extracting colormap and gray/alpha information

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
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Extracting colormap and gray/alpha information

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting colormap and gray/alpha information

Post by snibgo »

For question 2: rgba also works for greyscale images with alpha. You'll get the same values for R, G and B.
snibgo's IM pages: im.snibgo.com
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: Extracting colormap and gray/alpha information

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting colormap and gray/alpha information

Post 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?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extracting colormap and gray/alpha information

Post 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)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting colormap and gray/alpha information

Post 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".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extracting colormap and gray/alpha information

Post 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:
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: Extracting colormap and gray/alpha information

Post 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
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: Extracting colormap and gray/alpha information

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracting colormap and gray/alpha information

Post 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".
snibgo's IM pages: im.snibgo.com
timholy
Posts: 13
Joined: 2013-03-04T07:34:13-07:00
Authentication code: 6789

Re: Extracting colormap and gray/alpha information

Post 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?
Post Reply