I want to scan a collection of rectangular photos, both color and black-and-white. I am generally quite happy with the scanning, but I run into some practical problems, due to the fact that photos are placed by hand and may not be well aligned with the scanning canvas (or the edges of the scanner)

I assume that my photo has been scanned into a white background rectangle containing the rectangular picture I want, possibly not aligned with the edge of the white rectangle.

Is there an application (preferably running on Linux) that can rotate automatically the scan so that the picture edges are horizontal/vertical? If it also crop out the surrounding white background, it is even better (though proper Unix design would separate this issue).

I know how to do that by hand with Gimp, checking the rotation with my eyes. But I would like to have it done automatically.

To make things clearer with actual images: given the first image below with the irregular white border, I would like to automatically extract the second one, without any surrounding white.

enter image description here               enter image description here

Note: I had to add the dashed-line to show the edges of the first image. If you want to test a proposed solution for the automatic extraction, you should rather use this image.


3 Answers 3


Sounds like you're looking for ImageMagick. This is typically installed by default with a lot of the more popular distros. If not you can use your distros package manager to install it.

Normal image rotation

$ sudo yum install ImageMagick


$ sudo apt-get install ImageMagick

ImageMagick includes many CLI tools, one of which is convert. Using convert you can rotate images like so:

$ convert -rotate "90" in.jpg out.jpg

There are many more examples on using convert here. The rest of the tools included with ImageMagick can do image editing, scaling, and annotating.


The other method for rotating images that are typically off a bit vertically or horizontally is called deskewing.

From the ImageMagick online command line options help:

-deskew threshold{%}
straighten an image. A threshold of 40% works for most images.

Use -set option:deskew:auto-crop true | false to auto crop the image.

To use this option, you'd use the convert CLI tool that's another of ImageMagicks CLI tools:

 $ convert skewed.jpeg -deskew 40% not_skewed.jpg


  • 1
    Ah. You want to deskew the image see here stackoverflow.com/q/41546181/33204
    – slm
    Jul 9, 2018 at 12:03
  • Thanks again. That seems to be it. I did not know the technical word. Still, I am a bit surprised since skewing normally denotes some kind of distortion, while in this case there is no distortion, only a rotation. Can I be sure it will only rotate my image, witout distorting it? Another point is that this option is not listed on the imagemagick page imagemagick.org/script/convert.php , though it is available on my Linux. Actually, I have some difficulty finding on the web a description or definition of deskew and what it does precisely. I am still looking ... nay suggestion?
    – babou
    Jul 9, 2018 at 13:16
  • @babou - I believe it's lossless, it looks like it's setting the deskew attribute in the image header itself - imagemagick.org/discourse-server/viewtopic.php?t=29229.
    – slm
    Jul 9, 2018 at 17:05
  • Is there really a standard deskew attribute, that would contain both the angle of rotation and the center of rotation? But this corresponds to an image that is not aligned with the canvas, i.e. with edges that are not horizontal or vertical. This is odd. Strange also that I cannot find an official definition of deskew in imagemagick, but only forum discussions.
    – babou
    Jul 9, 2018 at 23:12
  • @babou - I agree on both points. Haven't seen a definition, I'd have to look into the ImageMagick source to confirm the equation used to do it beyond the forum post.
    – slm
    Jul 9, 2018 at 23:15

I recently needed to digitalize the set of more than 500 photos made by film cameras. And needed to auto-trim and auto-rotate all of them.

Those parameters that worked better for me:

convert myFile.jpg -fuzz 30% -trim +repage -deskew 40% cropped_file.jpg

And here if you want to apply it to the entire folder:

mkdir cropped
for i in `ls *.JPG`; do convert $i -fuzz 30% -trim +repage -deskew 40% cropped/`basename $i JPG`jpg; done

By reading this blog post, I found this bash script which uses ImageMagick : download it, chmod +x it and then I managed to rotate & crop my scanned photos with the following command:

multicrop -c 50,50 -f 40 -d 50 scanned_photo_in.jpg extracted_photo_out.jpg
  • -c 50,50 is to detect the background color from pixel at 50,50 (default is 0,0)
  • -f 40 is the "fuzz value for separating background color expressed as percent" (default is 10)
  • -d 50 will discard any region that has a width or height smaller than this size (default is to not discard anything)

Here is a bash script to run this command for any jpg files in the current folder and put the extracted photos in extracted sub folder:

mkdir extracted; for PHOTO in *.jpg; do multicrop -c 50,50 -f 40 -d 50 $PHOTO extracted/$PHOTO; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.