How can I sort photos into color and black-and-white images?

Asked 3/5/2023

3 views

2 answers

0

I want a simple way to organize a photo collection by color versus black-and-white/monochrome. I expected there might be a metadata tag for this, but I haven’t found one. Is there a practical utility or method to identify grayscale images, and possibly also detect images that are effectively black and white but stored as RGB files?

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

Photography Stack Exchange contributor

3y ago

2 Answers

2

Experimentally, taking this picture, and saving four JPEGs:

  • one with full color
  • one with dulled colors (saturation=50%)
  • one with all colors reduced to grayscale (saturation=0%)
  • one which is really single-channel grayscale (not shown below)

enter image description here

The single grayscale one is easily identified:

  • the file utility reports only one component (three in the others)
  • ImageMagick's identify command reports a Gray colorspace (sRGB for the others)

For the other three, a main differentiator is to count the unique colors, by design a grayscale JPEG image (whether true grayscale or RGB image with R=G=B) can have at most 256 colors when others are usually in the hundred thousands[*]. Using identify

$ for f in *.jpg ; do printf "%14s: %6d\n" $f $(identify -format "%k" $f) ; done
Chroma-000.jpg:    256
Chroma-050.jpg: 126123
Chroma-100.jpg: 345665
 Grayscale.jpg:    256

Using this it is easy (at least on OSX or Linux) to build a small script to move files depending on color count, for instance:

find . -name '*.jpg' -exec bash -c 'count=$(identify -format "%k" $1) ; [[ $count -le 256 ]] && exit 0 ; exit 1 ;' _ {} \;  -print

only print the names of the grayscale picture, and something like

find . -name '*.jpg' -exec bash -c 'count=$(identify -format "%k" $1) ; [[ $count -le 256 ]] && exit 0 ; exit 1 ;' _ {} \;  -exec mv -t /path/to/dir {} +

will move them to directory /path/to/dir.

[*] This may not be true for formats with high bit depths, for instance a grayscale 16-bit PNG could have 65536 colors (but 16-bot PNG aren't that frequent and can be discriminated with the file command or else). This is still less than the colors in the average photo. This also couldn't be true for CGI, but then we are on the Photography site.

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

user75947

3y ago

0

AI Answer

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

There usually isn’t a reliable built-in metadata tag for “color vs black-and-white.” The practical approach is to inspect the image data.

For true grayscale files, many tools can detect them directly because they use a single channel or gray colorspace. For example, file/identify (ImageMagick) can report whether an image is Gray rather than sRGB/RGB.

For black-and-white-looking images stored as RGB, detection is harder. A grayscale RGB image has equal R, G, and B values for each pixel, so script-based checks can identify those. ImageMagick can also help: grayscale images typically have far fewer unique colors than normal color photos.

File size can sometimes be a clue: true grayscale files are often smaller than equivalent RGB files, especially in uncompressed formats. But this is not reliable for JPEGs, because compression varies with image content.

So, if you want an automated workflow:

  1. Detect true grayscale files by channel count/colorspace.
  2. For RGB files, use a script to test whether channels are equal (or nearly equal if you want to catch “almost grayscale” images).

If you’re comfortable scripting, a small Python tool is a good solution for bulk sorting.

UniqueBot

AI

3y ago

Your Answer