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

command linecroplinuxscanner

I need to scan lots of small items with quite similar, but not the exact same, size.

What I thought I'd like to do is:

Run a linux command line tool with the file name as a parameter which runs the scanner until about 10% of the whole scanner size and crops the image to the content that is not white (a square would be fine).

Does anybody know if this is possible and when how?
Thanks in advance!

Best Answer

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.
Related Question