- 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, but I've not been able to find anything online about the colormap.
Code: Select all
convert $filename -channel Index -separate -depth $bitdepth gray:-
- I know how to get out data in one "go" as an array, using lines such as
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.
Code: Select all
convert $filename -depth $bitdepth $colorspace:-
Extracting colormap and gray/alpha information
Extracting colormap and gray/alpha information
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:
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extracting colormap and gray/alpha information
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
Re: Extracting colormap and gray/alpha information
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extracting colormap and gray/alpha information
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
- 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
I am confused about what you mean by index and colormap. The command
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)
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.convert $filename -channel Index -separate -depth $bitdepth gray:-
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)
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extracting colormap and gray/alpha information
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".
I don't recall seeing "-channel index" documented, but it is listed in "convert -list channel".
snibgo's IM pages: im.snibgo.com
- 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
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
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
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
...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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Extracting colormap and gray/alpha information
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".
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
Re: Extracting colormap and gray/alpha information
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?