Question

I have a directory that contains a very large number of JPG images (scans of paper documents), which are all currently sRGB.

Many but not all of them don't actually have color, or have color only as the background (e.g. colored paper or slightly off-white scan).

I would like to mass-convert those images (but not images that have real color) to grayscale, to save disk space.

I'm guessing that there is some way to do this with ImageMagick, but I don't know how. Suggestions?

Was it helpful?

Solution

Imagemagick's mogrify utility would be the simplest solution.

mogrify -path /path/to/output/dir/ -colorspace Gray /path/to/source/files

The ability to identify which document qualifies as as real colors may be very localized. I'd suggest evaluating the color count if each image, and only convert the images that drop below a given threshold.

THRESHOLD=1024
FILES=`ls /path/to/source/files`
for FILE in ${FILES}; do
COLORS=`identify -format %k "$FILE"`
if [[ $COLORS -lt $THRESHOLD ]]; then
  mogrify -path /path/to/output/dir/ -colorspace Gray "$FILE"
fi

It may also be wise to evaluate pre-processing options to reduce noise. Monochrome would be the easiest to apply, but other great solutions exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top