How to scan an image and automatically crop it to the scanned content with a linux command line tool?

What you need is convert from imagemagick. First install imagemagick for your distribution. On debian derived systems run this command:

sudo apt-get install imagemagick

Now, if you just want to remove whitespace do this:

for image in $(find . -name "*png" | sed 's/.png//'); do convert -trim $image.png $image_trimmed.png; done

This assumes your images are PNGs, if not change the above line accordingly.

If you need fancier resizing, have a look at the imagemagick documentation, you can do just about anything you can imagine with it.

So, your actual workflow would be:

  1. Scan your images and save them in the same folder.
  2. Run the command I gave above in that folder.

Try adding -fuzz:

-fuzz *distance*

Colors within that distance are considered equal.

for image in $(find . -name "*png" | sed 's/.png//');
do convert -fuzz 255 -trim $image.png $image_trimmed.png; done