How to : Rotate image assets, EXIF metadata!

On occasion, users may upload image data to Labelbox to find that the assets unexpectedly appear rotated in both Catalog and in labeling projects. The reason this happens is because of underlying EXIF metadata on the images.

Many devices, such as smartphones, will automatically re-orient a photograph to a more natural state. For example, if you hold your phone in landscape (horizontal) view and take a picture of a person, if you open your camera roll, the photo will appear in portrait mode. If I text the photo to a friend, they’ll receive it in portrait mode.

Let’s illustrate what happens with a real example!

  1. I take a photo of my friend Andrés Giménez, starting second basemen for the Cleveland Guardians. Note how I turn my phone to take the photo:

  2. When I open the photo I just snapped, or upload it to this post, observe how it is orientated. The image is adjusted to a sensible orientation by my browser/the community platform:

  3. Now, if I open the metadata for this image, I can see that when my phone captured the photo, it set the Orientation value to 8:

  4. Next, I’ll upload the image to Labelbox. Something’s changed!

The EXIF orientation data has been stripped from my image! Fortunately, this is intentional, expected behavior. Let’s jump into what’s happening here and why.


The reason that we strip the Exif metadata is to guarantee your annotations are always drawn and stored at the same orientation, ensuring data integrity. If we were to rotate the image in our platform, the annotations created on the image would not align with how the data is stored. Additionally, for security purposes, under no circumstances can Labelbox write to a cloud storage bucket to change the orientation of an asset; thus, Labelbox cannot perform this orientation modification.

When a photo is taken by a camera not in its natural orientation at the time of capture, an orientation flag is added so that on various browsers and phones, the asset is displayed on a screen in the same view with which the camera captured the image.

Below is a summary of how these flags work. In our example above, the Orientation flag was 8, and we experienced the same exact rotation and subsequent stripping of the rotation shown in the graphic.
exif (1)


So, now that we have a better understanding of EXIF metadata, how can I force the re-orientation of my assets so that they appear how I want them to in Labelbox?

If you do not wish to annotate the images in their non-natural orientation, you must remove the orientation flag and then ensure the image is rotated and stored as desired. Just removing the orientation attribute could leave the image in its non-natural rotated state and therefore require extra steps to fix it.

The Pillow package contains the class ImageOps with the method exif_transpose() to do everything for us (source: ImageOps Module).

  1. Install the Pillow module:
pip install Pillow
  1. Run the following code with the relevant source_image and target_image values:
from PIL import Image, ImageOps

source_file = "exif_image.jpeg"
target_file = "exif_image_transposed.jpeg"

image = Image.open(source_file)
transposed_image = ImageOps.exif_transpose(image)
transposed_image.save(target_file)

If your data is frequently captured with camera phones, I recommend inserting this logic into your upload pipeline so that you can ensure all assets have their orientation flag removed and are rotated into their natural state.

For the above reasons, Labelbox will not rotate images into a natural state on-demand like a camera phone may do for ease of viewing. We’ve experimented with alternative approaches, but the inability to ensure data integrity between the asset and created annotations is of the utmost priority, and this has been supported by the feedback we’ve received from ML practitioners over the years.

2 Likes