Batch-processing images of documents to look like a fax

faxgradientimagemagickscanner

Let's say I have a photo or scan of a text document, possibly with
some low-contrast watermark background. If it is a photo, in addition
to the watermark there will be brightness gradients from the lighting
and possibly from the sheet of paper not laying flat due to folds.

I want to postprocess these photos with imagemagick to look like a
fax, i.e. converting the image to monochrome black/white correcting
local variations of brightness. The normal -threshold option will
not work, since

  1. It doesn't automatically detect the required brightness level for
    each photo.
  2. Due to the brightness gradients the text on one part of the image
    may be brighter than the background on another part such that for
    any given global threshold some text will be lost.

Cam-Scanner apps on smartphones usually provide a black/white-document
option that corrects such color gradients and calculates a reasonable
first guess for the threshold value, that would suffice for batch
processing.

They do not help though when I have the raw image on PC already,
though I could in theory upload them to the smartphone and import them
— it is just highly impractical, especially for large numbers of images.

Does imagemagick or some other batch-processing capable software
(preferably open-source) support such a conversion?

Best Answer

You can use Imagemagick's Mathematical Compose Methods to achieve such results. Divide_src [1] in particular as it would remove any gradients, vignettes, unwanted shadings.

Then a -normalize and a -threshold should do the rest.

convert $input -colorspace gray ( +clone -blur 15,15 ) -compose Divide_Src -composite -normalize -threshold 80% $output

Here's my result:

You may want to adujst the threshold to get the best results.

Depending ot the OS you are going to run this you might have to escape the brackets: "\(" and "\)".

As for the batch processing personally I would use a "for" loop either in bash or in Cygwin again depending on the OS:

for file in test/*; do convert $file -colorspace gray ( +clone -blur 15,15 ) -compose Divide_Src -composite -normalize -threshold 80% result/`basename $file`; done

However there is another command line tool you might want to check out called mogrify [2] for inline or specific -path batch processing.

For more information and possibly different results follow [3] and [4].


[1]: www.imagemagick.org/Usage/compose/#divide

[2]: www.imagemagick.org/script/mogrify.php

[3]: staff.washington.edu/corey/camscan/

[4]: www.imagemagick.org/Usage/photos/#color-in

Related Question