EXIF Orientation primer

Michael Seifert, 2019-01-09

tl;dr

When we take a picture with a photo camera, we expect the resulting image to be in the same orientation as we intended. This means landscape pictures are displayed horizontally and portrait pictures are displayed vertically on screen. But how does the computer (or the camera) know how an image has been shot?

Most image formats contain not only the content, but additional information on the content: the image metadata. The JPEG image format may contain metadata in the Exchangeable Image File Format (EXIF). Among things like the lens and camera model used to shoot the picture, EXIF may contain a field for the image orientation.

EXIF orientation values for "regular" devices

The canonical orientation is 1. An orientation value of 1 means that the image content exists exactly in the way we want to view it on the screen. Let's assume we have photo camera whose sensor has an aspect ratio of 4:3. When we use the camera to shoot an image in landscape orientation, the resulting JPEG image will have an orientation value of 1. When we shoot in portrait mode, however, the camera's sensors notice that the orientation has changed. The resulting image's EXIF metadata will have an orientation value of 8, if the camera was rotated clockwise, or 6, if the camera was rotated counter-clockwise. Likewise, 3 is the corresponding orientation for upside-down landscape pictures.

There are cases where the camera produces a flipped image, due to restrictions of the camera's hardware or shortcomings of the firmware. For this reason, there exist another four possible orientation values as depicted in the following image:

EXIF orientation values for devices producing horizontally flipped images

Interestingly, none of the eight possible values account for the camera being pointed directly upwards or downwards.

The EXIF orientation makes it possible to show an image to the user as it was intended by the photographer. At the same time, it complicates image processing on the software side: For one, it is necessary to manipulate both image data and EXIF data in unison. When the image rotation and the EXIF orientation are out of sync, the image will not be displayed correctly. Secondly, it is often desired to remove EXIF metadata entirely from an image. This is especially true when performing image optimization to achieve faster load times on websites. Website performance is generally dependent on page weight, i.e. the amount of data that needs to be transferred before the browser can render the website. Stripping EXIF information from images reduces page weight without compromising image quality. However, images need to be treated properly in advance.

The handling of EXIF orientation in JPEG images used to be vastly inconsistent a few years ago. Luckily, we now have better tools to deal with the issue today.

The script exifautotran11 The script is packaged with Debian's libjpeg and also installed by other distros, e.g. Gentoo was built for exactly this purpose. It determines the EXIF orientation and transforms the image accordingly. The transformation is performed using the low-level tool jpegtran which is provided by libjpeg. This has the added advantage that the transformation is potentially lossless.22 Lossless JPEG transformations are only possible, if the image dimensions are an integer multiple of the block size

$ exifautotran myimage.jpeg

If you cannot use exifautotran for some reason, you can still use ImageMagick. It's convert tool provides an -auto-orient switch to normalize the EXIF orientation:

$ convert -auto-orient myimage.jpeg normalized.jpeg

Support for orientation normalization in Ameto is currently in the testing stage and will be rolled out soon.

Summary

EXIF orientation denotes how a camera was rotated when an image was shot. Image viewers use the orientation value in order to present the image with the correct orientation. The intermingling of image content and metadata increases requirements on the software side: 1) EXIF metadata and image content need to be manipulated together. 2) EXIF metadata takes up a significant chunk of total image size. Therefore, it is often desirable to strip images of all or most of the metadata. This, in turn, means that one needs to take special care when applying image optimization techniques. Tools like exifautotran or ImageMagick provide ways to normalize image orientation.