Why Your Photo Rotates Itself: EXIF Orientation Explained

You take a portrait photo. It looks fine on your phone. You email it, and the recipient sees it lying on its side. Nobody rotated anything. The explanation is a single number stored alongside the image โ€” and once you understand it, a whole family of confusing photo problems becomes obvious.

What is actually happening

Your camera sensor is fixed in the phone's body, so it always captures a landscape image regardless of how you hold it. Rather than rotating millions of pixels, the camera writes a small metadata tag โ€” EXIF Orientation โ€” saying "turn this 90ยฐ when displaying". Software that reads the tag shows it upright. Software that ignores it shows the raw sideways data.

Why cameras do this

A phone's image sensor is soldered to the board in one fixed orientation. When you turn the phone, the sensor turns with it โ€” but it still reads out pixels in the same order relative to itself. Rotate the phone 90ยฐ and the scene lands sideways on the sensor.

The camera could rotate the pixel data before saving. It does not, for two good reasons:

  • Speed. Rotating a 48-megapixel image takes real time and battery. In burst mode, shooting several frames a second, that cost is prohibitive.
  • Quality. A JPEG is compressed in 8ร—8 pixel blocks. Rotating means decoding, rotating and re-encoding โ€” losing a little quality on every save.

Writing a single byte of metadata is instant and lossless. The design is sound. The problem is that honouring it is optional, and for twenty years a great deal of software simply did not bother.

The eight orientation values

ValueMeaningTo display correctlyHow common
1NormalNothingVery common โ€” landscape shots
2Mirrored horizontallyFlip horizontallyRare
3Upside downRotate 180ยฐCommon โ€” phone held inverted
4Mirrored verticallyFlip verticallyRare
5Mirrored and rotated 90ยฐ CCWFlip, then rotateVery rare
6Rotated 90ยฐ CCW by the cameraRotate 90ยฐ clockwiseVery common โ€” portrait shots
7Mirrored and rotated 90ยฐ CWFlip, then rotateVery rare
8Rotated 90ยฐ CW by the cameraRotate 270ยฐ clockwiseCommon โ€” portrait, other way up

In practice only 1, 3, 6 and 8 occur with any frequency. The mirrored values exist because the specification was designed for scanners and film digitisers as well as cameras, where a negative could genuinely be fed in backwards. They occasionally appear from front-facing cameras that mirror the preview.

๐Ÿ’ก Why the numbers are not in a sensible order

The values are not arbitrary โ€” each encodes a transformation of the two image axes. Values 1 to 4 preserve which axis is which, while 5 to 8 swap rows and columns. Within each group the variants cover the mirroring options. It is a compact encoding of the eight ways to map a rectangle onto itself, which is mathematically tidy and completely unintuitive to read.

Which software honours the tag

SoftwareHonours orientation?
Phone photo galleriesAlways
Modern browsers (<img>)Yes โ€” default since around 2020
Windows Photos, macOS PreviewYes
Photoshop, Lightroom, GIMPYes, usually with a prompt
HTML <canvas> drawingNo โ€” raw pixels only
Most server-side image librariesUsually not unless told to
Older desktop viewersOften not
Email clientsInconsistent
Some CMS thumbnail generatorsFrequently not

This table explains the most common version of the problem: a user uploads a photo that looks perfectly upright in their gallery, the server generates a thumbnail with a library that ignores EXIF, and the thumbnail comes out sideways while the full-size image โ€” displayed by the browser, which does honour it โ€” looks correct. Same file, two different renderings, on the same page.

โš ๏ธ Canvas strips orientation silently

Any browser-based image processing that draws to a <canvas> gets the raw pixel data, with the orientation tag not applied. So an image that displays correctly in an <img> tag comes out sideways the moment you resize or crop it via canvas โ€” and because the output is new pixel data with no EXIF, it stays sideways forever.

The fix is to read the orientation first and apply the matching transform to the canvas context before drawing, or to use createImageBitmap with { imageOrientation: 'from-image' }, which handles it for you.

The double-rotation problem

Here is the sequence that produces a photo which is wrong in a new way after you tried to fix it:

  1. Photo has sideways pixels and Orientation = 6. Tag-aware software shows it upright.
  2. You open it in an editor that ignores EXIF. It shows sideways.
  3. You rotate it 90ยฐ and save. The pixels are now upright.
  4. The editor leaves Orientation = 6 in the file, because it never read it.
  5. Tag-aware software now rotates the already-upright pixels another 90ยฐ.

The photo is now correct in the software that was wrong before and wrong in the software that was right. Rotating again just swaps which half of your applications are broken.

โœ… The only stable fix

Rotate the pixels to upright and set Orientation to 1. Once the raw data is correct and the tag says "no rotation needed", every piece of software agrees โ€” those that read the tag and those that ignore it. This is what a proper "auto-orient" function does, and it is why doing it by hand so often fails.

Fixing it properly

Lossless rotation โ€” the right way for JPEG

JPEG compresses in 8ร—8 blocks. Those blocks can be rearranged and rotated without decoding them, which means a rotation with no quality loss at all. jpegtran does exactly this:

# Rotate pixels losslessly and reset the tag in one step exiftran -ai *.jpg # Or with jpegtran, rotating by a known amount jpegtran -rotate 90 -copy all -outfile out.jpg in.jpg # ImageMagick: apply the tag to the pixels, then clear it magick input.jpg -auto-orient output.jpg # In bulk magick mogrify -auto-orient *.jpg

One caveat on lossless rotation: it only works cleanly when the image dimensions are multiples of 8 (or 16 with chroma subsampling). Where they are not, the edge blocks cannot be rearranged, and tools either trim a few pixels or fall back to re-encoding. For camera photos, which have conveniently round dimensions, this rarely comes up.

Reading and setting the tag

# What does this file actually say? exiftool -Orientation -Orientation# photo.jpg # Orientation : Rotate 90 CW # Orientation : 6 # Set the tag without touching pixels โ€” only correct if # the pixels are already right exiftool -Orientation=1 -n photo.jpg # Check a whole folder at once exiftool -Orientation# -filename -r ./photos

Handling it in code

// JavaScript โ€” let the browser do the work const bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' // applies EXIF for you }); ctx.drawImage(bitmap, 0, 0); // CSS โ€” for an img element only img { image-orientation: from-image; } /* the default now */
# Python โ€” Pillow handles all eight cases from PIL import Image, ImageOps img = Image.open('photo.jpg') img = ImageOps.exif_transpose(img) # rotates pixels, clears the tag img.save('fixed.jpg', quality=95)
// Node.js with sharp โ€” .rotate() with no argument means auto-orient await sharp('photo.jpg') .rotate() // reads EXIF and applies it .resize(1200) .toFile('thumb.jpg');

๐Ÿšจ Always auto-orient before resizing on a server

If you resize first and orient afterwards, you have already produced a sideways thumbnail โ€” and resizing usually discards the EXIF, so there is nothing left to correct with. .rotate() before .resize(), every time. This one line ordering is behind a large share of sideways-avatar bugs.

The privacy interaction

EXIF carries more than orientation. It commonly includes GPS coordinates, the exact date and time, the camera's serial number, and sometimes the owner's name. Stripping it before publishing a photo is sound practice.

But stripping metadata removes the Orientation tag along with everything else. If the pixels were sideways and relying on the tag, the photo is now permanently sideways in every application, with no way to recover the original intent.

# WRONG โ€” orientation is lost with the GPS data exiftool -all= photo.jpg # RIGHT โ€” bake the rotation into the pixels first, # then remove everything magick photo.jpg -auto-orient -strip clean.jpg # Or, keeping only orientation while removing the rest exiftool -all= -tagsfromfile @ -Orientation photo.jpg

Strip metadata without breaking your photos

Remove GPS coordinates, timestamps and camera details before you share โ€” in your browser, so the photo is never uploaded anywhere.

Open the EXIF Remover โ†’

Which formats carry orientation

FormatSupports EXIF orientation?Notes
JPEGYesWhere the problem overwhelmingly occurs
HEIC / HEIFYesApple's default photo format
TIFFYesEXIF originated as a TIFF extension
WebPYes, optionallySupport varies by encoder
AVIFYesUses its own transformation properties
PNGNoNo orientation concept โ€” pixels are always as stored
GIFNoSame
BMPNoSame

This gives a useful diagnostic shortcut: converting a JPEG to PNG bakes in whatever the converter decided. If the converter honoured the tag, the PNG is upright forever. If it did not, the PNG is sideways forever. Either way the ambiguity is gone โ€” which is occasionally the quickest way to settle a stubborn case.

Diagnosing a specific file

  1. Read the tag. exiftool -Orientation# photo.jpg. If it returns 1 or nothing, EXIF is not your problem.
  2. Compare two viewers. Open the file in a browser and in something that ignores EXIF. Different results confirm the diagnosis immediately.
  3. Check where in your pipeline it breaks. Upload, thumbnail generation, and display are three separate stages and any one can be the culprit.
  4. Fix at the source. Auto-orient on upload, once, before anything else touches the file. Every downstream stage then works with unambiguous data.

Summary

  • Cameras store photos in sensor orientation plus a tag saying how to turn them โ€” it is faster and lossless.
  • Only values 1, 3, 6 and 8 appear in practice.
  • Honouring the tag is optional, which is why the same file looks different in different software.
  • Canvas ignores it, so browser-based editing silently produces sideways output.
  • Fix both the pixels and the tag. Changing one without the other creates the double-rotation problem.
  • Auto-orient before stripping metadata, or you lose the orientation permanently.
  • Auto-orient before resizing on a server, never after.
  • Use lossless rotation for JPEG so quality is not lost on every fix.

Frequently Asked Questions

Why does my photo look correct on my phone but sideways on my computer?

The image data itself is sideways. Your camera stored the picture in the sensor's native orientation and added an EXIF Orientation tag saying how to turn it. Software that reads that tag displays it correctly; software that ignores it shows the raw sideways data. Nothing is wrong with the file โ€” the two programs simply disagree about whether to honour the tag.

What are the 8 EXIF orientation values?

1 is normal, 3 is 180 degrees, 6 is rotate 90 clockwise to display, and 8 is rotate 270 clockwise. Values 2, 4, 5 and 7 add mirroring and are rare outside front-facing cameras. In practice you will only ever see 1, 3, 6 and 8.

Why does rotating my photo not stick?

Some editors rotate by changing the EXIF flag rather than the pixels. That looks fixed in software that honours the tag and unchanged in software that ignores it. Others rotate the pixels but leave the old tag in place, so tag-aware viewers apply the rotation a second time and the photo ends up wrong in the opposite direction.

Does removing EXIF data fix or break orientation?

It usually breaks it. Stripping metadata for privacy removes the Orientation tag along with the GPS coordinates, so every viewer falls back to the raw pixel data โ€” and if those pixels were sideways, the photo is now permanently sideways everywhere. Always apply the rotation to the pixels before stripping metadata.

How do I fix orientation without losing quality?

Use a lossless JPEG rotation, which rearranges the compressed 8x8 blocks without decoding and re-encoding. The jpegtran utility does this with -rotate, and several image tools offer it as 'lossless rotate'. Rotating in a normal editor and re-saving re-compresses the whole image and loses a little quality every time.

P

Written by Paras

We build free, browser-based file tools and write the reference material we wish existed when we were looking things up. Spotted an error? Tell us and we will fix it.