Threshold & separate outputs a bilevel rather than a grayscale

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
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Threshold & separate outputs a bilevel rather than a grayscale

Post by tom_dl »

When I threshold a channel and then use -separate to save only this channel, the image I get is in 'bilevel' mode (acording to identify), rather than grayscale.

I use this line:

Code: Select all

convert input.png -colorspace lab -channel G -separate -threshold 50% -channel G -separate output.png
And obviously I'm expecting an image with only 2 colors (black + white) so I guess IM is choosing bilevel as it is all that's necessary. However, when compositing multiple images of this type, I get strange results, where the images regain their channel identity, and produce colored images.

How can I fix the output type as 'Grayscale'?
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Re: Threshold & separate outputs a bilevel rather than a grayscale

Post by tom_dl »

I've tried:

Code: Select all

convert input.png -colorspace lab -channel G -separate -threshold 50% -channel G -separate -type grayscale output.png
to no avail, by the way.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Threshold & separate outputs a bilevel rather than a grayscale

Post by fmw42 »

I am not sure I understand what your ultimate goal is. Can you explain what you are trying to do functionally step-by-step. Your command above does not make sense to me. Once you have separated a channel, there is no reason to separate it again.

try

Code: Select all

convert input.png -colorspace lab -channel G -separate +channel -threshold 50% output.png
That is going to make one bilevel output since it only has two colors, black and white. That thresholded image with be the A channel in LAB, but the result will lose any information about the fact that it came from LAB colorspace.

After this step, what is you are trying to do next? What are you expecting from the final result.

If you only want to threshold one channel of the LAB image and keep the rest the same, then

Code: Select all

convert input.png -colorspace lab -channel R -threshold +channel -colorspace sRGB result.png
This will threshold the L channel of LAB and keep the others unchanged and then convert back to sRGB. Note that it is not a good idea to change the AB channels non-symmetrically. That will cause weird color shifts.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Threshold & separate outputs a bilevel rather than a grayscale

Post by snibgo »

I point out the problems in your near-identical thread viewtopic.php?f=1&t=27109
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: Threshold & separate outputs a bilevel rather than a grayscale

Post by fmw42 »

snibgo wrote:I point out the problems in your near-identical thread viewtopic.php?f=1&t=27109

Yes, I forgot to add +channel in one of my examples on the other page. Also you are right about -set colorspace. I was just trying to convert back to sRGB at the end.

My error. You are correct.
Post Reply