How can I detect whether an RGB JPEG is effectively black-and-white from the command line?

Asked 6/25/2015

4 views

2 answers

0

I have JPEG files stored as RGB, but some are actually black-and-white (or very close to it). I want a command-line way to identify which images are grayscale rather than full color. Is there a practical way to do this with tools like ImageMagick or GraphicsMagick?

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

11y ago

2 Answers

3

You could start by converting the image to grayscale and compareing that to the original image. If the image is already black and white, you'd expect the two images to be very similar; if it's full color, you'd expect them to be different.

Clarification: Given the two comments below, I think I wasn't clear enough here. Certainly, the grayscale version of a B&W image stored as RGB will likely not be exactly the same as the original image, but it should be pretty close. Subtracting the grayscale version from the original will yield a dark frame with a faint ghost of the original image. You could threshold this image to get a completely black frame. A full color image, on the other hand, will be significantly different from its grayscale counterpart, and subtracting the latter from the former will give an image that still has a lot of color even after thresholding (exactly what colors depends on how the grayscale conversion is done).

Another option: Thinking about the problem some more, another option is to convert the image to a neutral white point and then compare the red, green, and blue channels. In a color image, these will have significant differences; in a black and white image, they should be very similar. Again, they might not be exactly the same, depending on how the image was acquired in the first place, but the differences should be small enough that they can be easily filtered out. This is effectively the same idea as the one above, but the approach is perhaps more straightforward.

Originally by user4262. Source · Licensed CC BY-SA 4.0

user4262

11y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

Yes. A practical approach is to test whether the image has little or no color variation, even if it is stored as RGB.

Two common methods from the command line are:

  1. Compare the image to a grayscale conversion. If an RGB image is truly black-and-white, converting it to grayscale should change it very little. A color image will differ much more.

  2. Inspect channel statistics with ImageMagick. identify -verbose image.jpg can show per-channel statistics; for a grayscale image, the red, green, and blue channels should be nearly identical. You can also examine saturation in an HSL/HSV-style colorspace: for a true grayscale image, saturation should be essentially zero throughout.

In practice, allow a small tolerance because JPEG compression, toning, or scanning artifacts may make channels slightly different even in visually black-and-white images. So instead of requiring exact equality, classify images as grayscale if channel differences or saturation stay below a threshold.

UniqueBot

AI

11y ago

Your Answer