Convert color photos of documents to good black-and-white (bitonal) images?

Since I don't have a copier or scanner, I'm using an 8 megapixel camera to copy documents. This works pretty well except they need a lot of processing afterward. I'd like to get from a photo to a bitmap, but using

djpeg -grayscale -pnm photo.jpg |
pgmtopbm -threshold -value XXX

does not work so well, for two reasons:

  1. It's hard to guess what XXX should be, and XXX is different for different photos.

  2. Illumination varies, and sometimes a single threshold isn't what's right for the image.

How can I do better? The ideal solution will be fully automatic command-line program that I can run on Linux. (I have already written a program to remove dark pixels from the edges of images.)

NOTE: I really want a bitmap, that's just black and white pixels. No grayscale, no dithering.


-monochrome

This option uses some smart dithering and generates very visible output:

convert -monochrome in.png out.png

Documentation: http://www.imagemagick.org/Usage/quantize/#monochrome

Compare that to a simpler -threshold 50 transform:

convert -threshold 50 in.png out.png

which loses most of the image.

Concrete example from: https://www.nasa.gov/mission_pages/galex/pia15416.html

wget -O orig.jpg http://www.nasa.gov/images/content/650137main_pia15416b-43_full.jpg
# Downsize to 400 height to have a reasonable file size for upload here.
convert orig.jpg -resize x400 in.jpg
convert -monochrome in.jpg out.jpg
convert -threshold 50 in.jpg threshold-50.jpg

in.jpg

enter image description here

out.jpg

enter image description here

threshold-50.jpg

enter image description here

Related questions:

  • https://stackoverflow.com/questions/15861025/how-to-convert-an-image-to-1bit-px-binary-bitmap-with-imagemagick-or-rmagick
  • https://askubuntu.com/questions/9868/convert-an-image-from-grayscale-to-binary
  • https://unix.stackexchange.com/questions/108613/how-do-you-binarize-a-colored-image

Tested in Ubuntu 19.10, ImageMagick 6.9.10.


The best thing I've found in three years is the mkbitmap program that ships with potrace.