EXIF Orientation and Upscale Order: All 8 Tags Are Lossless, So Order Does Not Matter — Except When It Silently Does
The standard advice is "normalise orientation before you upscale, or you will resample twice." I measured it on all eight EXIF orientation values and the advice is wrong for every one of them: each tag is an exact pixel permutation that changes 0.00% of pixels. The ordering still matters for non-right angles, but only by about 0.8 dB. What actually destroys images is a dropped tag and a hardcoded output size — 55.6% aspect error, and a loss of 12.6 dB that no resolution check will catch.
Why "rotate before upscale" sounds right, and why it is mostly a myth
The reasoning is intuitive. A rotation by an arbitrary angle resamples the grid: every output pixel is an interpolation of four input neighbours, so if you rotate first and upscale second, the interpolation error gets amplified along with everything else. Do it the other way and the interpolation happens once, at the final resolution, where the error is a fraction of a pixel instead of a fraction of a pixel blown up by 2×.
That argument is correct — for arbitrary angles. It is not correct for the eight orientations that cameras actually write, because those are all multiples of 90° plus optional mirroring, and a multiple of 90° is not an interpolation at all. It is a rearrangement of memory. Whether you reshuffle before or after a scale is irrelevant if the reshuffle is exact, and the measurement below shows it is exact to the bit.
Experiment 1 — all eight EXIF orientation values, tested for losslessness
I generated an 83×57×3 uint8 array of uniform random noise. Random noise is the right test signal here: any interpolation, rounding or resampling shows up immediately as changed pixels, where a smooth gradient could hide a sub-level error. Each of the eight orientation values was applied through Pillow's transpose operation and compared against an independent numpy implementation of the same permutation.
| EXIF tag | Meaning | Output size | Pixels changed | Bit-exact |
|---|---|---|---|---|
| 1 | normal | 57×83 | 0.00% | yes |
| 2 | mirror horizontal | 57×83 | 0.00% | yes |
| 3 | rotated 180° | 57×83 | 0.00% | yes |
| 4 | mirror vertical | 57×83 | 0.00% | yes |
| 5 | mirror + 90° CCW | 83×57 | 0.00% | yes |
| 6 | rotated 90° CW | 83×57 | 0.00% | yes |
| 7 | mirror + 90° CW | 83×57 | 0.00% | yes |
| 8 | rotated 90° CCW | 83×57 | 0.00% | yes |
Every value is exact. Not "visually lossless", not "within quantisation error" — the output array is bit-identical to the permuted input, on all 14.193 pixels, for all eight tags. That means the ordering question for real camera files has an answer before any upscaling is considered: it does not matter. Rotating a 4000×3000 phone photo and then upscaling it produces the same pixels as upscaling it and then rotating, to the last bit.
Where I got it wrong first. My initial reference implementation used
np.rot90 with the wrong sign for tags 6 and 8, which made Pillow look lossy. Pillow
was correct; my test was not. I only caught it because the mismatched tags were exactly the two
pure rotations and their mirrors were passing — an asymmetry no real codec would produce.
Experiment 2 — the ordering premium, measured on angles that are not right angles
Since EXIF tags are a non-issue, the interesting question is the general one: for a rotation that genuinely interpolates, how much does ordering cost? I built a 2048×1536 detailed target (Siemens star plus rendered text), downsampled it 2× with a box filter to create the "source", and compared two pipelines against an ideal constructed at each pipeline's own output size.
The size-matched ideal matters. My first version of this test compared both pipelines against a single reference and reported a 1.9 dB advantage for upscale-first. That number was inflated: the rotate-first path had been resampled once more just to reach the reference grid, so it was penalised for a step the other path never performed. Comparing each output to the best image obtainable at its own dimensions removes that artefact. The honest number is smaller, and it is the one in the table.
| Angle | rotate → upscale | upscale → rotate | Δ | HF retained (up→rot) |
|---|---|---|---|---|
| 1° | 27.904 dB | 28.630 dB | +0.726 | 41.67% |
| 5° | 28.318 dB | 29.208 dB | +0.890 | 41.87% |
| 15° | 29.452 dB | 30.301 dB | +0.849 | 42.92% |
| 30° | 30.500 dB | 31.277 dB | +0.777 | 45.26% |
| 45° | 30.740 dB | 31.528 dB | +0.788 | 46.20% |
Upscaling first wins by 0.73–0.89 dB across every angle tested, and it also retains more high-frequency energy: 38.3–39.3% for rotate-first versus 41.7–46.2% for upscale-first. The advantage is remarkably flat with angle, which is the expected signature — the cost is one interpolation pass either way, and the only difference is the scale at which rounding error is committed. A 0.8 dB gap is real but small; it will not rescue a soft source, and it will not be visible on a phone screen.
The practical consequence is uncomfortable but simple. For the eight EXIF values the premium is exactly zero, so the advice to "always normalise first" buys nothing. For a straightening rotation of a few degrees it buys about 0.8 dB, which is worth having but is not the difference between a usable and an unusable file. Time spent reordering a pipeline is better spent on the things in the next two sections.
Experiment 3 — what ffmpeg actually does with an orientation tag
Here the measurement turned from uninteresting to damaging. I built a 400×600 JPEG whose
pixels are stored portrait but whose EXIF carries Orientation=6, meaning a viewer
must rotate 90° CW to display it — it should appear as 600×400, aspect ratio 1.5. Three routes
through ffmpeg were tested, and the aspect ratio of each output compared against 1.5.
| Route | Output | Aspect error | Bytes |
|---|---|---|---|
| source file (tag 6) | 400×600 | — | 40.546 |
default + scale=800:1200 | 800×1200 | 55.6% | 34.632 |
default + scale=800:-2 | 800×534 | 0.1% | — |
-noautorotate + scale=800:1200 | 800×1200 | 55.6% | 28.922 |
| EXIF stripped, then upscale | 800×1200 | 55.6% | 27.036 |
ffmpeg's default autorotate fires on input and hands the filter graph a 600×400 frame. The
filter then obeys the hardcoded scale=800:1200 literally and stretches that already
landscape frame back into a portrait one — a 55.6% aspect error, on a file that ffmpeg itself
rotated correctly one step earlier. Nothing errors, nothing warns, and the output has a perfectly
plausible size. Only scale=800:-2, which derives height from the true aspect, survives
the autorotate.
The stripped-EXIF route shows the second failure mode. It is the same 55.6%, but for a different reason: with the tag gone, the autorotate has nothing to act on, so the stored portrait pixels go straight through and the image is simply sideways. Its 27.036 bytes are also the smallest of the group, which is a useful hint — if a pipeline suddenly produces notably smaller files, check orientation before assuming a codec improvement.
Experiment 4 — the silent 12.6 dB loss
To put a quality number on it, I ran the full phone-to-upscale path: a controlled ground truth at 1200×800, stored as pixels rotated so that orientation 6 restores it, downsampled 2× and saved as JPEG with the tag set. Four routes were compared against the correctly oriented ideal.
| Route | PSNR | HF energy | Verdict |
|---|---|---|---|
| rotate pixels → upscale | 26.165 dB | 45.88% | correct |
| upscale → rotate pixels | 26.164 dB | 45.87% | correct |
| ffmpeg autorotate (default) | 26.143 dB | 48.08% | correct |
ffmpeg -noautorotate | 13.505 dB | 60.12% | sideways |
The first three are within 0.02 dB of each other. That is the losslessness result from experiment 1 showing up again in a realistic pipeline: the ordering is irrelevant, and ffmpeg's own autorotate is as good as doing it by hand. The fourth route loses 12.6 dB.
Note the HF figure for that route: 60.12%, the highest in the table. Higher is not better here. The energy is high because the image is compared against a reference 90° away from it, so every edge boundary falls in the wrong place and the Laplacian fires on the mismatch. This is a trap in detail-retention metrics generally — a badly aligned image can score higher on HF energy than a correct one. PSNR is the metric that caught it; HF alone would have reported the broken output as the sharpest of the four.
Experiment 5 — double rotation, and why shape checks cannot find it
The third failure is the most embarrassing because it survives every sanity check people
actually run. If a library autorotates at decode and the code then also calls
exif_transpose(), the image is rotated twice — 180° from correct.
Measured on the same noise array: the double-rotated result differs from the correct one at a PSNR of 7.8 dB, and both are the same shape, 57×83. A dimension assertion passes. A file-size check passes. A "did the image load" check passes. An automated thumbnail review passes, because a 180° rotation of a photograph still looks like a photograph. The only reliable detectors are a visual look at an asymmetric feature (which is why I put coloured corner blocks in the test files) and a PSNR comparison against a known reference — and in production nobody has the reference.
Experiment 6 — the export step is where orientation actually dies
Everything above rests on a precondition I never stated: the tag has to still be there when the file is read. Experiments 1–5 all opened a file that had just been written by the same process, so the tag was guaranteed present. Real pipelines break that guarantee at the export call, not at the decode call, and the failure is silent because the pixels are never touched.
Two questions decide whether this matters. First, is the tag fragile in the formats people actually write? Second, if the tag is lost, what does that cost in pixels?
6.1 The tag itself is not fragile
I wrote all eight orientation values into four container formats and read them back: JPEG (quality 95), PNG, lossless WebP and TIFF. All eight values round-trip in all four formats — 32 of 32, eight distinct values read back per format, no collapse to orientation 1.
| Format | Values recovered | Distinct | Verdict |
|---|---|---|---|
| JPEG q95 | 8/8 | 8 | tag survives |
| PNG | 8/8 | 8 | tag survives |
| WebP lossless | 8/8 | 8 | tag survives |
| TIFF | 8/8 | 8 | tag survives |
Storing the tag is also nearly free. A JPEG with no EXIF block at all was 3.104 bytes; writing any EXIF block with the orientation tag made it 3.140 bytes, a flat +36 bytes. The orientation value itself costs 0 extra bytes — 274=1 and 274=6 produce byte-identical file sizes, because the value is a two-byte field inside a block that already exists. There is no size argument for dropping it.
6.2 The tag is lost by omission, not by codec
The way orientation dies is the ordinary "load, process, save" call where nobody propagates the EXIF block. Pillow does not carry it forward unless you pass it:
from PIL import Image
src = Image.open("sideways.jpg") # getexif()[274] == 6, size 96x64
src.save("out.jpg", quality=95) # no exif= argument
# out.jpg: getexif().get(274) is None, size still 96x64
import numpy as np
from PIL import ImageOps
# rendered as-is, compared to the correctly oriented reference:
# 99.24% of pixels differ (coloured corner markers end up in the wrong corners)
That is the whole bug: tag dropped, pixels never rotated, 99.24% wrong, and the file size and dimensions are unchanged. Nothing about the output looks suspicious to a script. The aspect ratio is still 96×64 because nothing re-encoded the geometry — the picture is simply sideways now, permanently, with no tag left to say so.
The control case confirms the fix is cheap: transposing before saving gives a 64×96 file, tag
none, and a pixel mismatch of 2.62% against the reference — that residue is just
JPEG quantisation and the resample to the reference grid, not a geometry error. Order of operations
does not matter here as long as exactly one of the two carries the rotation: either
exif_transpose() and then save, or save and keep the tag.
6.3 ffmpeg autorotates by default, and strips the tag while doing it
ffmpeg 6.1.1 is the tool I recommended in the pipeline section, so I checked what it does to a tagged file rather than assuming. Default behaviour is correct: it applies the orientation at decode, writes upright pixels, and drops the tag because the tag is no longer needed.
| Invocation | Output size | Tag after | Pixel mismatch | Aspect error |
|---|---|---|---|---|
| default (autorotate on) | 64×96 | none | 1.53% | 0% |
-noautorotate before -i | 96×64 | none | 99.19% | 125% |
default + scale=192:-2 | 192×288 | none | 1.69% | 0% |
-noautorotate + scale=192:-2 | 192×128 | none | 99.28% | 125% |
Two practical notes came out of running this rather than reading the docs. -noautorotate
is an input option: placing it after -i makes ffmpeg exit 234 with
"cannot be applied to output url" and write no file at all — a hard failure, which at least is
honest. And the aspect error is exactly the 125% figure: a 0.667 true ratio becomes 1.500, the
portrait shot becomes a landscape one, and scale=192:-2 faithfully preserves the wrong
shape because -2 derives only the height it was told to derive.
6.4 A result that was my own error
My first run of this experiment reported a 99.58% mismatch for ffmpeg's default path, which
would have meant ffmpeg was mangling orientation. It was not. My fixture had been built with
REF.transpose(Image.ROTATE_270) as the stored orientation for tag 6, and the sign was
wrong — the correct stored pixels for tag 6 are ROTATE_90.
The fix was to stop trusting my own fixture and add a gate to the test: a file is only a valid
orientation-6 fixture if ImageOps.exif_transpose() on it reproduces the reference. That
is not the tool under test, it is the definition of what an EXIF-aware reader is supposed to do, so
it makes a clean oracle. With the gate in place, three of the four candidate stored orientations
failed it (99.58%, 99.20%, 92.30%) and only ROTATE_90 passed at 1.53%. Every number in
the table above comes from the gated fixture. The earlier 99.58% is in this article because it is
the exact error this whole page is about — a check that looked like a tool bug and was actually a
sign error in the test.
6.5 What this changes about the advice
It sharpens it, and it moves the emphasis. The ordering question that experiments 2–4 spent most of their effort on is worth 0.73–0.89 dB at odd angles and nothing at the right angles cameras actually write. The export question is worth 99% of the pixels, permanently, and it is decided by one keyword argument that most code does not pass.
- Assert the tag state you intend, on the output. Either the output has no tag and upright pixels, or it has a tag and stored pixels. Having neither is the failure mode, and it trips no dimension check.
- Pass the EXIF block explicitly when it must survive.
save(..., exif=...)for Pillow,-map_metadata 0orcopy_metadatain ffmpeg. The default for a freshsave()is to drop it. - Gate any orientation test on a reference library. The bug I hit — a wrong transpose sign in the fixture — produces results indistinguishable from a real library bug. If the test and the tool disagree, rebuild the fixture before touching the tool.
The reproducible pipeline
Everything above is one script. The orientation check is the part worth keeping:
from PIL import Image, ImageOps
import numpy as np
im = Image.open("phone.jpg")
tag = im.getexif().get(0x0112, 1)
print("orientation tag:", tag) # 6 on most portrait phone shots
# Normalise exactly once. ImageOps.exif_transpose is a pure permutation,
# so this costs nothing and cannot be lossy.
fixed = ImageOps.exif_transpose(im) # tag is cleared on the result
assert "Orientation" not in fixed.getexif()
fixed.save("stage.jpg", quality=95) # NO exif= argument: tag must not survive
upscaled = fixed.resize((fixed.width*2, fixed.height*2), Image.LANCZOS)
# Guard against the double-rotate bug:
assert upscaled.width == fixed.width * 2
assert upscaled.height == fixed.height * 2
Three rules follow from the numbers, and they are cheap enough to apply everywhere:
- Normalise once, at the start, and assert the tag is gone. All eight values are lossless permutations, so this step costs exactly zero detail. It removes the ambiguity that causes every other failure in this article.
- Never write a literal output size.
scale=800:1200is correct only for one specific input geometry. Use-2for the derived dimension, or compute the target fromfixed.width/heightafter normalisation. - Do not trust shape or file size as an orientation check. A sideways orientation-6 file and a correct one are both 800×1200; a double-rotated file is the same shape as a correct one. Only the tag and a pixel comparison distinguish them.
For the record, ordering was the hypothesis I expected to matter most, and it turned out to be the least important variable measured here — worth 0.8 dB at odd angles and exactly nothing at the right angles that cameras write. The aspect-ratio bug and the sideways pass-through were worth 55.6% and 12.6 dB respectively. Effort belongs on those two, not on reordering steps that are already exact.