DEV Community

Peiguan
Peiguan

Posted on

Will Your Photo Come Out Sideways? A Python EXIF Orientation Preflight Before You Pay for a Bama Bound Clip (Tested)

Checked November 17, 2026 (15:19–15:21 CST, UTC+8). I build bamabound.app, an independent studio that turns one photo of one person into a vertical clip of them holding a red I'M BAMA BOUND banner. It is not the University of Alabama, and the clip is not an admission letter.

The photo rule on the FAQ and the photo guide is short: one JPEG, PNG or WebP of one person, up to 10 MB, with the face uncovered. There is no prompt. Resolution is the only extra setting. The site does not publish a required aspect ratio, and it does not mention EXIF Orientation.

That last gap is the one that bites people who write their own "is this photo okay?" check before they pay. A phone held upright often stores the pixels as a landscape rectangle and writes Orientation 6 or 8. A naive width, height = Image.open(path).size then reports 1280×720 for a picture that displays as 720×1280. If your script rejects "landscape" uploads on that number, you will fail a perfectly ordinary portrait. If it only looks at the stored size, it can also miss the opposite mistake.

I wrote a local preflight that never uploads anything. It reads the file, applies ImageOps.exif_transpose, and tells you whether a size check that ignored the tag would have lied. Then I built a small fixture set and ran it.

What the tag actually does

EXIF Orientation 1 means the stored pixels are already the right way up. Orientation 3 is a 180° rotation: the axes stay put, so a 720×1280 file is still 720×1280 after a compliant viewer rotates it. Orientation 6 and 8 are the 90° tags. Those do swap the axes. The fixtures that honour the usual phone convention store those two as 1280×720 and display them as 720×1280.

The preflight flags exif_orientation_would_fool_a_naive_size_read when the displayed picture is portrait and the stored pixels, read without the tag, look landscape (or the other way around).

The preflight

Enter fullscreen mode Exit fullscreen mode


python

!/usr/bin/env python3

"""Preflight a still before you pay to upload it to a one-photo studio.

Checks: format, file size, pixel size, EXIF Orientation vs how Pillow opens
the file, and whether a naive dimension read (without applying Orientation)
would lie about portrait vs landscape.

This script never uploads anything. It only reads a local file.
"""
from future import annotations

import argparse
import json
import sys
from pathlib import Path

from PIL import Image, ImageOps, UnidentifiedImageError

ALLOWED = {"JPEG", "PNG", "WEBP"}
MAX_BYTES = 10 * 1024 * 1024
MIN_SHORT_SIDE = 256 # a practical floor, not a site rule

def exif_orientation(img: Image.Image) -> int | None:
try:
exif = img.getexif()
except Exception:
return None
if not exif:
return None
# 0x0112 is Orientation
value = exif.get(0x0112)
"format_allowed": False,
"mode": None,
"width_stored": None,
"height_stored": None,
"orientation": None,
"width_displayed": None,
"height_displayed": None,
"orientation_swaps_axes": False,
"looks_portrait_displayed": None,
"looks_portrait_if_you_ignore_exif": None,
"short_side": None,
"issues": [],
"ok_for_upload_shape": False,
}
if not rec["exists"]:
rec["issues"].append("missing_file")
return rec
rec["over_10mb"] = rec["bytes"] > MAX_BYTES
if rec["over_10mb"]:
rec["issues"].append("over_10mb")
try:
with Image.open(path) as img:
rec["format"] = img.format
rec["mode"] = img.mode
rec["width_stored"], rec["height_stored"] = img.size
rec["orientation"] = exif_orientation(img)
applied = ImageOps.exif_transpose(img)
rec["width_displayed"], rec["height_displayed"] = applied.size
except UnidentifiedImageError:
rec["issues"].append("unreadable_image")
return rec
except OSError as e:
rec["issues"].append(f"open_error:{e}")
return rec

rec["format_allowed"] = rec["format"] in ALLOWED
if not rec["format_allowed"]:
    rec["issues"].append("format_not_jpeg_png_webp")

rec["orientation_swaps_axes"] = (
    rec["width_stored"], rec["height_stored"]
) != (rec["width_displayed"], rec["height_displayed"])
rec["looks_portrait_displayed"] = rec["height_displayed"] >= rec["width_displayed"]
rec["looks_portrait_if_you_ignore_exif"] = rec["height_stored"] >= rec["width_stored"]
rec["short_side"] = min(rec["width_displayed"], rec["height_displayed"])
if rec["short_side"] < MIN_SHORT_SIDE:
    rec["issues"].append("short_side_under_256")
if rec["orientation"] in (5, 6, 7, 8) or rec["orientation_swaps_axes"]:
    if rec["looks_portrait_displayed"] != rec["looks_portrait_if_you_ignore_exif"]:
        rec["issues"].append("exif_orientation_would_fool_a_naive_size_read")
rec["ok_for_upload_shape"] = (
    rec["format_allowed"]
    and not rec["over_10mb"]
    and "unreadable_image" not in rec["issues"]
    and "short_side_under_256" not in rec["issues"]
)
return rec
Enter fullscreen mode Exit fullscreen mode

def main() -> int:
p = argparse.ArgumentParser(description=doc)
p.add_argument("paths", nargs="+", type=Path)
p.add_argument("--json", action="store_true")
args = p.parse_args()
rows = [inspect(path) for path in args.paths]
if args.json:
json.dump(rows, sys.stdout, indent=2)
sys.stdout.write("
")
else:
for rec in rows:
flag = "OK " if rec["ok_for_upload_shape"] and not rec["issues"] else "SEE"
print(
f"{flag} {rec['path']}
"
f" {rec['bytes']} bytes format={rec['format']} "
f"stored={rec['width_stored']}x{rec['height_stored']} "
f"displayed={rec['width_displayed']}x{rec['height_displayed']} "
f"Orientation={rec['orientation']}
"
f" issues={rec['issues'] or ['none']}"
)
return 0

if name == "main":
raise SystemExit(main())

Enter fullscreen mode Exit fullscreen mode


python

MIN_SHORT_SIDE = 256 is a practical floor I put in the script so a obviously tiny test file gets a warning. It is not a published site rule. The live FAQ and guide only state JPEG, PNG or WebP, up to 10 MB. A 200-pixel photo might still be accepted by the upload field; I would not use one, but I will not pretend the studio published a 256 px minimum.

A short fixture builder

A short fixture builder

The full fixture kit I used also writes a 73 MB JPEG so the 10 MB check has something to trip over. I am not shipping that file, and the script below does not create it. Point the oversize test at any JPEG larger than 10,485,760 bytes if you want that row. Everything else you need is here: the four orientation tags, a portrait and a landscape with no EXIF, PNG, WebP, a TIFF, and an 80×142 thumbnail.

Enter fullscreen mode Exit fullscreen mode


python

!/usr/bin/env python3

"""Build a small set of orientation / format fixtures. Does not write an oversized JPEG."""
from future import annotations

from pathlib import Path

from PIL import Image, ImageDraw

OUT = Path("fixtures")
W, H = 720, 1280 # same frame as a 9:16 720p clip

def upright() -> Image.Image:
img = Image.new("RGB", (W, H), (236, 232, 224))
d = ImageDraw.Draw(img)
d.rectangle((0, 0, W, 80), fill=(20, 20, 20))
d.rectangle((0, H - 120, W, H), fill=(165, 20, 30))
d.text((24, 24), "TOP", fill=(255, 255, 255))
d.text((24, H - 80), "BOTTOM BANNER", fill=(255, 255, 255))
return img

def save_jpeg_with_orientation(img: Image.Image, path: Path, orientation: int) -> None:
# Store pixels without applying the tag. Compliant viewers rotate on open.
exif = Image.Exif()
exif[0x0112] = orientation
exif[0x0131] = "bama-exif-preflight-fixture"
img.save(path, format="JPEG", quality=92, exif=exif, subsampling=0)

def main() -> None:
OUT.mkdir(parents=True, exist_ok=True)
base = upright()
# Inverse-transform so ImageOps.exif_transpose restores the upright picture.
# EXIF 6: stored as landscape (1280x720); displayed 720x1280.
# EXIF 8: the other 90° tag. EXIF 3 is 180° and does not swap axes.
inv = {
1: lambda im: im,
3: lambda im: im.transpose(Image.ROTATE_180),
6: lambda im: im.transpose(Image.ROTATE_270),
8: lambda im: im.transpose(Image.ROTATE_90),
}
for orient, fn in inv.items():
save_jpeg_with_orientation(fn(base), OUT / f"orient-{orient}.jpg", orient)
base.save(OUT / "no-exif-portrait.jpg", format="JPEG", quality=92)
land = Image.new("RGB", (1280, 720), (236, 232, 224))
ImageDraw.Draw(land).text((24, 24), "LANDSCAPE", fill=(20, 20, 20))
land.save(OUT / "no-exif-landscape.jpg", format="JPEG", quality=92)
base.save(OUT / "portrait.png", format="PNG")
base.save(OUT / "portrait.webp", format="WEBP", quality=90)
base.resize((80, 142)).save(OUT / "too-small.jpg", format="JPEG", quality=92)
base.save(OUT / "portrait.tif", format="TIFF")
print(f"wrote fixtures in {OUT}")
for p in sorted(OUT.iterdir()):
print(f" {p.name:28} {p.stat().st_size:9} bytes")

if name == "main":
main()

Enter fullscreen mode Exit fullscreen mode


markdown

What I measured

I ran exif_preflight.py on the fixtures. Displayed size is after ImageOps.exif_transpose.

File Stored Displayed Orientation Issues
orient-1.jpg / orient-1-upright.jpg 720×1280 720×1280 1 none
orient-3.jpg 720×1280 720×1280 3 none (180°, axes not swapped)
orient-6.jpg 1280×720 720×1280 6 exif_orientation_would_fool_a_naive_size_read
orient-8.jpg 1280×720 720×1280 8 same issue
no-exif-portrait.jpg 720×1280 720×1280 None none
no-exif-landscape.jpg 1280×720 1280×720 None none
portrait.png / portrait.webp 720×1280 720×1280 None none
portrait.tif 720×1280 720×1280 None format_not_jpeg_png_webp (2,764,940 bytes, format TIFF)
too-small.jpg 80×142 80×142 None short_side_under_256
over-10mb.jpg 4000×6000 4000×6000 None over_10mb (73,278,492 bytes, 69.89 MiB)

A few things worth spelling out.

Orientation 3 is the easy one to over-think. The picture is upside down if you ignore the tag, but the stored size is already 720×1280, and the displayed size is the same. The script does not flag it as a naive-size lie, because a width/height check would not have been fooled. You still need exif_transpose (or a viewer that honours the tag) if you want the head at the top.

Orientation 6 and 8 are the ones that fool a size read. Stored 1280×720, displayed 720×1280. Both are allowed formats, both are under 10 MB, and both came back ok_for_upload_shape=true with that one issue noted. I would upload the transposed, upright JPEG if I were being careful, but the live pages do not require you to strip the tag first.

The landscape file with no EXIF is also not rejected. The script does not treat landscape as a failure, because the site asks for one person, not a required aspect. A group photo still does not create extra people in the clip; that is a different rule, on the FAQ, and this preflight does not try to detect it.

TIFF is rejected on format alone: format_not_jpeg_png_webp. The studio lists JPEG, PNG and WebP. A 2,764,940-byte TIFF of the same portrait does not become allowed just because it is under 10 MB.

The oversize fixture was 73,278,492 bytes, 4000×6000. The check is a raw byte count against 10 * 1024 * 1024, not a megapixel cap.

Flattening a copy, if you want to

If you prefer to upload a file whose stored size is already the displayed size, transpose and save a new JPEG with Orientation 1 (or with no Orientation tag):

from pathlib import Path
from PIL import Image, ImageOps

src = Path("phone-photo.jpg")
with Image.open(src) as im:
    upright = ImageOps.exif_transpose(im)
    upright.save(src.with_name(src.stem + "-upright.jpg"), format="JPEG", quality=92)
Enter fullscreen mode Exit fullscreen mode

That is a local convenience, not a published requirement. The live FAQ and guide do not ask you to strip EXIF first. They ask for one JPEG, PNG or WebP, up to 10 MB, with the face uncovered.

How I would use it

Run it on the photo you are about to upload, locally:

python3 exif_preflight.py portrait.jpg
python3 exif_preflight.py --json fixtures/*.jpg fixtures/*.png fixtures/*.webp fixtures/*.tif
Enter fullscreen mode Exit fullscreen mode

If you see exif_orientation_would_fool_a_naive_size_read, open the file in anything that honours EXIF (Preview, Photos, a browser, or Pillow with exif_transpose) and confirm the person is upright. Saving a flattened copy with Orientation 1 is a reasonable habit. It is not a published requirement.

If you see format_not_jpeg_png_webp or over_10mb, that does match the published rule. Convert or compress before you pay. The studio still does the real check at upload time; this script is only a preflight.

One photo, face uncovered, then choose a resolution if you want more than the 480p default. That part has not changed.

Top comments (0)