Tuomas Siipola Articles Projects

Exif orientation in different formats

Exif orientation is known to cause problems and over time similar issues have cropped up in different circumstances. Web browsers and other software are slowly getting better at handling Exif orientation, and nowadays, there shouldn't be too many problems with JPEGs. Other image file formats are, however, a different story.

Why would an image even use Exif orientation? I can think of three reasons:

  1. Normally it's added by a camera to differentiate landscape and portrait shots.
  2. It's copied while converting image from one file format to another.
  3. It's added or modified by some image preview application, image editor or utility like exiftool or exiv2.

Next, let's investigate Exif orientation in different image file formats.

JPEG

According to the Exif specification section 4.7.2 JPEG stores Exif metadata in APP1 marker where data starts with the string "Exif\0\0". Here are JPEG test images which should look the same if your browser handles Exif orientation properly:

WebP

WebP container format stores Exif metadata in EXIF chunk. For example, running cwebp with -metadata exif copies Exif metadata from the source image. However, at the time of writing, no browser seems to support the orientation tag in WebP. Test your browser below:

PNG

Exif metadata extension for PNG was standardized in 2017 with the introduction of eXIf chunk. An earlier nonstandard way of storing Exif metadata was using zTXt chunk with keyword "Raw profile type exif". At the time of writing, no browser seems to support the standard eXIf chunk, but interestingly Safari supports the nonstandard way. Test your browser below:

AVIF

AV1 Image File Format, or AVIF for short, is a promising image file format currently supported only in Google Chrome by default. It's based on the HEIF container format which is prominently used by Apple. According to the HEIF standard, HEIF files can store Exif metadata in Exif item, but the format also has a built-in way of transforming images: irot property rotates the image 0, 90, 180 or 270 degrees and imir property flips the image vertically or horizontally.

It's unclear whether you should use Exif orientation or the built-in transformations. For instance, iOS creates HEIF files with both Exif orientation and irot property containing equivalent values. At the time of writing, neither option is supported by browsers in AVIF images. Test your browser below:

Handling Exif orientation

Inspired by Dave Perrett's work, I've created a collection of CC0-licensed test images to check Exif orientation support in different file formats. You can browse the images or download everything as a zip file. Similarly to the images in this article, software that support Exif orientation should display these images in the same orientation.

For publishing images on the web, you should rotate and flip the image data instead of relying on Exif orientation. You can do this with -auto-orient option of ImageMagick:

magick source.jpg -auto-orient destination.jpg

If you're developing an image library or application, here are the operations required to rotate and flip the image data according to the Exif orientation value from 1 to 8:

  1. no nothing
  2. flip horizontally
  3. rotate 180 degrees
  4. flip vertically
  5. rotate 90 degrees clockwise and flip horizontally
  6. rotate 90 degrees clockwise
  7. rotate 90 degrees clockwise and flip vertically
  8. rotate 270 degrees clockwise

In practise, you're likely to encounter images with incorrect Exif orientation. To work around this, your application could provide an interface to manually verify and correct the orientation after loading an image.

When saving images, you may want to strip Exif metadata altogether from the output file. This will obviously remove the orientation tag but also make the file smaller and remove any personal information stored in the metadata. However, if you need the Exif metadata, it's also possible to remove only the orientation tag or set its value to 1.

This article focused mainly on image formats popular on the web. For more information on different formats and the metadata they may store, check out ExifTool's Tag Names and Exiv2's Metadata reference tables and Image Metadata and Exiv2 Architecture. Many image formats, including those listed in this article, can store XMP metadata which might include Exif properties like orientation. Fortunately, this doesn't seem to be widely used or even supported, so we don't need to worry about it.