The dumb reason your fancy Computer Vision app isn’t working: Exif Orientation

How Digital Cameras Auto-Rotate Images

When you take a picture, the camera will sense which end you have tilted up. This is so the picture will appear in the correct orientation when you look at it again in another program:

Why does this break so many Python Computer Vision Applications?

Exif metadata is not a native part of the Jpeg file format. It was an afterthought taken from the TIFF file format and tacked onto the Jpeg file format much later. This maintained backwards compatibility with old image viewers, but it meant that some programs never bothered to parse Exif data.

Google Vision’s API demo fails to rotate a portrait-oriented image captured with a standard cell phone.
Finder on a Mac always displays images with Exif rotation applied. There is no way to see that the image data is actually sideways inside the file.

Fixing the Problem

The solution is that whenever you load images in your Python programs, you should check them for Exif Orientation metadata and rotate the images if needed. It’s pretty simple to do, but surprisingly hard to find examples of code online that does it correctly for all orientations.

pip3 install image_to_numpy
import matplotlib.pyplot as plt
import image_to_numpy
# Load your image file
img = image_to_numpy.load_image_file("my_file.jpg")
# Show it on the screen (or whatever you want to do)
plt.imshow(img)
plt.show()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Geitgey

Adam Geitgey

Interested in computers and machine learning. Likes to write about it.