DEV Community

Ali Ahmed
Ali Ahmed

Posted on Edited on Originally published at aliahmed.si

Why your OCR sees ID photos sideways: EXIF orientation, and fixing it with Pillow

A phone held upright doesn't rotate the pixels it saves. It stores the image in the camera sensor's orientation, often landscape, and adds an EXIF Orientation tag (value 6, for example, means "rotate 90° clockwise to display"). Photo viewers and browsers apply the tag, so the user sees an upright document. OCR and face detection read the raw pixels, so they get the document sideways: text lines run vertically and recognition quality collapses. The fix is one line with Pillow, ImageOps.exif_transpose(img), applied before anything else touches the image. It has to come first, because re-encoding or stripping metadata before applying it loses the orientation permanently. Tested Python below.

Three panels: the phone stores sideways pixels with EXIF Orientation 6; a photo viewer applies the tag and shows the card upright; OCR ignores the tag and reads the card sideways. The fix is ImageOps.exif\_transpose(img) before anything else.

Why mobile uploads look fine to people and sideways to OCR.

What the pipeline actually receives

I simulated a phone photo: an upright, portrait ID card with a dark header at the top-left, stored as landscape pixels plus Orientation = 6.

orient.py: what the code sees

from io import BytesIO
from PIL import Image

naive = Image.open(BytesIO(data))
naive.size                      # (480, 300): landscape, though the user photographed a portrait card
naive.getexif().get(0x0112)     # 6: "rotate 90° clockwise to display"
dark_corner(naive)              # 'bottom-left': the header the user saw at the top-left
Enter fullscreen mode Exit fullscreen mode

A browser shows this image upright, so a developer checking uploads by eye sees nothing wrong. The model sees the card rotated by a quarter turn.

The fix

orient.py: load_upright

from io import BytesIO
from PIL import Image, ImageOps

def load_upright(data: bytes) -> Image.Image:
    """Open an uploaded photo and apply its EXIF orientation, so pixels match what the user saw."""
    img = Image.open(BytesIO(data))
    img = ImageOps.exif_transpose(img)  # rotates/flips per the tag, then removes the tag
    return img.convert("RGB")

fixed = load_upright(data)
fixed.size                   # (300, 480): portrait
fixed.getexif().get(0x0112)  # None: the tag is removed, so nothing downstream rotates it twice
dark_corner(fixed)           # 'top-left': as the user saw it
Enter fullscreen mode Exit fullscreen mode

exif_transpose handles all eight orientation values, including the mirrored ones that front cameras can produce, and removes the tag afterwards. That second part matters: if you rotated the pixels but kept the tag, a viewer would apply it again, and the image would be wrong in every viewer instead.

Why order matters

Pipelines often "clean" uploads first: re-encoding to strip metadata for privacy, or resizing. Do that before applying the orientation, and the tag is gone:

orient.py: re-encoding first

buf = BytesIO()
naive.convert("RGB").save(buf, "JPEG")  # re-encoded without EXIF
dark_corner(load_upright(buf.getvalue()))  # 'bottom-left': sideways forever, nothing left to fix it
Enter fullscreen mode Exit fullscreen mode

The test asserts exactly this. So the order is: apply orientation → then strip metadata, resize, re-encode. The same applies to the upload validation in checking magic bytes in Node.js: re-encode images, but only after orienting them.

What about photos with no tag?

Screenshots, scans and images that have already been through a "strip EXIF" tool have no tag, and they can still be sideways if the user held the document rotated. EXIF can't help there. Detect it from the content instead: an OCR engine's orientation detection (Tesseract's OSD, for example), a text-line direction check, or, for ID documents, the position of the face or the MRZ. Rotate by the detected amount, and treat low confidence as a reason to ask for a new photo.

A tag can also be present but wrong, for example when an editing tool changed the pixels without updating it. Applying it then rotates a correct image the wrong way, with nothing downstream to notice. Where that risk matters, treat EXIF and the content check as two independent signals: trust the orientation when they agree, and send a disagreement to review instead of picking one. (A reader on DEV raised this case.)

Where this sits in the pipeline

  1. Validate the upload's type and size.
  2. Apply the EXIF orientation.
  3. Find and flatten the document, and reject blur and glare: preparing ID card photos for OCR.
  4. Hash it for duplicate detection: perceptual hashing for reused ID photos.
  5. OCR, MRZ parsing and face matching.

Skipping step 2 shows up as mysteriously poor OCR on mobile uploads only, which is a confusing bug to chase from accuracy metrics alone.

Summary

  • Phones store sensor-oriented pixels plus an EXIF Orientation tag; viewers apply it, models don't.
  • Call ImageOps.exif_transpose() first. It rotates the pixels and removes the tag.
  • Stripping metadata or re-encoding before that loses the orientation permanently.
  • For images without a tag, detect rotation from the content.

Related: why OCR struggles with non-Latin scripts and face match thresholds.


Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article

Top comments (6)

Collapse
 
aifliproom profile image
AI Flip Room •

Good point on the order of operations. We run the same rule on room photos: apply the orientation first, then resize and strip metadata. Do it the other way round and a portrait shot silently turns into a sideways one for every later step.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

Same rule, same reason. The nasty part is that the wrong order looks fine to anyone checking by eye in a browser, so it survives review.

Collapse
 
aifliproom profile image
AI Flip Room •

Yes, the browser quietly applies the rotation, so the reviewer sees an upright photo while the script gets a sideways one. One test image with a rotated EXIF tag kept in the fixtures catches it before anyone has to notice.

Collapse
 
tomerbarm profile image
Tomer Bar-Meir (Tom) •

Solid writeup — the exif_transpose-before-strip ordering is the detail most pipelines get wrong.

One more case worth flagging for the content-detection fallback: EXIF isn't only sometimes missing (screenshots, scans, stripped uploads) — it can also be present but wrong, e.g. after a crop/edit tool updates the pixels without updating the tag. Trusting it blindly in that case ships a confidently-wrong rotation with nothing downstream to flag it.

What's worked well building a dedicated rotation-detection API (BlueVetaUpright) is treating EXIF and a content-based model as two independent signals, and only trusting the answer when they agree — on disagreement, flag it as low-confidence instead of picking one blindly. For ID documents specifically, cross-checking against face/MRZ position like you mention is a solid third signal too.

Collapse
 
quantumbreakz profile image
Ali Ahmed •

Good point, and it was a case the post didn't cover: I only discussed a missing tag, not a wrong one. Treating EXIF and a content-based check as two signals, and sending disagreements to review instead of picking one, is the right shape. For ID documents the face or MRZ position gives a cheap content signal. I've added the "present but wrong" case to the post.

Collapse
 
tomerbarm profile image
Tomer Bar-Meir (Tom) •

Thanks for adding the case — the face/MRZ content signal is a good one for ID docs specifically, hadn't thought about anchoring on document-internal features like that.