Taking overhead pictures of documents and books with a smartphone or camera is often much faster than using a scanner. One of the downsides is, however, that the orientation sensor often gets confused by holding the camera horizontally and saves random orientation information in the Exif part of output file. Once imported to the PC, the orientation of individual images differs and one has to correct for this manually. In many cases, some images are still not properly oriented even after manually rotating them. Let’s fix this.
The way to fix this is to remove all orientation information in the exif part of the image file. This is easier said than done. On Linux, there’s exiftool, a great command line tool that can batch-remove exif tags in files. Unfortunately, only removing the ‘orientation’ tag doesn’t work in all cases. Having been a bit desperate at first, this made me remove just all exif information from images to get the desired result. But removing all exif information is a drastic overkill and removes information that could be worthwhile in the future such as the date and time the image was taken, the smartphone or camera model used, etc. So when I had a bit of time on my hand I dug a bit deeper to find out why just removing the ‘orientation’ tag did not work.
It turned out that some cameras save a thumbnail image inside a jpg file which has its own exif section. Exiftool just removes the orientation tag of the standard image but not of the thumbnail. After a bit of digging I found the proper command line options to remove
- The orientation information for the main image
- The thumbnail
- The exif information for the thumbnail
… from all jpg files in a directory:
exiftool -Orientation= -thumbnailimage= -ifd1:all= -overwrite_original *.jpg
And with this command, all images in a directory are finally oriented the same way. And for good measure, here are two commands to rotate all images in a directory by 90 degrees and to reduce their resolution:
# Resize for file in *.jpg; do convert $file -resize 50% $file; echo $file; done # Rotate 90 deg. right for file in *.jpg; do convert $file -rotate 90 $file; echo $file; done