Tech News
← Back to articles

Resizing images in Rust, now with EXIF orientation support

read original related products more articles

Resizing images in Rust, now with EXIF orientation support

Resizing an image is one of those programming tasks that seems simple, but has some rough edges. One common mistake is forgetting to handle the EXIF orientation, which can make resized images look very different from the original.

Last year I wrote a create_thumbnail tool to resize images, and today I released a small update. Now it’s aware of EXIF orientation, and it no longer mangles these images. This is possible thanks to a new version of the Rust image crate, which just improved its EXIF support.

What’s EXIF orientation?

Images can specify an orientation in their EXIF metadata, which can describe both rotation and reflection. This metadata is usually added by cameras and phones, which can detect how you’re holding them, and tell viewing software how to display the picture later.

For example, if you take a photo while holding your camera on its side, the camera can record that the image should be rotated 90 degrees when viewed. If you use a front-facing selfie camera, the camera could note that the picture needs to be mirrored.

There are eight different values for EXIF orientation – rotating in increments of 90°, and mirrored or not. The default value is “1” (display as-is), and here are the other seven values:

1 6 8 3 2 5 7 4 A diagram showing the eight different orientations of the word ‘FLY’: four rotations, four rotations with a mirror reflection.

You can see the EXIF orientation value with programs like Phil Harvey’s exiftool, which helpfully converts the numeric value into a human-readable description:

$ # exiftool's default output is human-readable $ exiftool -orientation my_picture.jpg Orientation : Rotate 270 CW $ # or we can get the raw numeric value $ exiftool -n -orientation my_picture.jpg Orientation : 8

... continue reading