← Home
To the index of computing posts
A common problem for me when taking photos of documents with my smartphone, is that the orientation of the picture gets messed up, probably because the phone is usually held in a horizontal position to take the picture of documents. It is then difficult for the phone to understand the right orientation of the picture. Viewing the pictures on a computer or the phone later on leads to the annoyance of having to reorient (turn it clockwise or counterclockwise) to be able to read it without getting neck injuries.
Pictures generally have two ways to be oriented: (1) On one hand there is the orientation of the picture itself, the way the camera has originally recorded it. (2) On the other hand, there is information stored within the meta tags of the picture. This is called EXIF data and aside of orientation contains information on the camera, compression method, picture size, etc.
The orientation information will tell the software used to view the image how to orient the picture. This information is, however, not always used by programs and this can lead to pictures being upside down or otherwise oriented in the wrong way, a thing that can lead to frustration and ange if the underlying problem is not understood.
For good housekeeping of pictures, it is advisable to orient the picture itself in the correct manner, meaning that the picture itself is turned instead of changing the information on the orientation in the meta data. To achieve this, following things are needed:
This will ensure that the pictures will always be shown in the right way, thus making the pictures portable and easy to use.
Viewer: The Mirage picture viewer is a simple software that will show and turn the picture in the actual orientation it was shot in. It also can turn the image whithout meddling with the EXIF data.
exiftool: It is a cli tool to manipulate the EXIF data of pictures.
$ exiftool -Orientation=1 -n *.jpg
This command will change the orientation tag in the EXIF data to “1” in all the files ending in “.jpg” in the current directory.
A partial readout of the Orientation tag in a directory with photos I took with my phone.
$ exiftool *.jpg | grep "Orientation"```
Orientation : Rotate 90 CW
Orientation : Rotate 90 CW
Orientation : Horizontal (normal)
Orientation : Rotate 90 CW
Orientation : Rotate 90 CW
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Rotate 90 CW
As I know that I mostly took the pictures in a way that would make the actual picture readable with the head tilted to the left, most of my pictures need to be turned counter-clockwise by 90° to be readable. Thus, I can save time by using a script I called rotate90cw.sh
, which will do exactly that by calling the cli program jpegtran, a tool that can losslessly exactly that. It can not do batch editing by itself, which is why it is necessary to use it in a script:
#!/bin/sh
############################################################
# #
# rotate90cw.sh #
# ============= #
# #
# Rotate pictures by 90 degrees in clockwise direction. #
# Using jpegtran. #
# #
############################################################
for f in *.jpg ; do
jpegtran -rotate 90 -copy all "$f" > "${f%.jpg}_rotated.jpg"
done
# The option "-copy all" preserves all meta tags.
# The rotated pictures are saved as new files with the appendage
# "-rotated.jpg".
Now, most of my pictures will be oriented correctly as new files with "_rotated.jpg" appended to them. Move these files to a new directory, the easiest way is like so:
mkdir rotated
mv *_rotated.jpg rotated/
move into the new directory:
cd rotated
I then proceed to use Mirage to check the rotated pictures for those that are not oriented in the correct manner.
Once this is done, I change the orientation tag to “1” using exiftool. Now, all the pictures have the same orientation tag and the actual orientation is correct too. The pictures should now be upright in any viewer. Here a readout of the orientation tag after the procedure:
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
Orientation : Horizontal (normal)
The original files are kept as a backup with the file name appendage "_original". If you are certain that everything went as expected, you can erase the backup files with
rm *_original
Pierre-Louis Blanchard, 2019-12-04, edited 2021-07-19
Last updated on: 2024-05-20