DEV Community

celticgemini001
celticgemini001

Posted on

I Expected Rotating Before Downscaling to Be Better. The Measurement Said No.

I went into this measurement with a confident prediction, and the data refused it. That is worth writing up precisely because the prediction was reasonable and the refutation was clean.

The question: if a pipeline has to both downscale and rotate an image, does the order matter for how much detail survives? The folk answer — and my own prior — is that downscaling first is better, because the rotation then has less data to interpolate. I set up an experiment to confirm that. Instead it showed four things, only one of which I expected:

  1. The order does change the output, measurably and reproducibly.
  2. The size of that change is small next to rotation's own cost — about 0.6 of a grey level.
  3. Both orders end up with more high-frequency energy than the no-rotation baseline, which means the extra energy is ringing, not recovered detail.
  4. My expected winner was the loser. Rotate-then-downscale retained more measured detail than downscale-then-rotate at every non-right angle.

That last result is the interesting one, so let me be careful about what it does and does not mean.

The setup, with the controls that make it valid

I took five product photographs, centre-cropped each to a square, and resampled to 2048×2048 so the pipeline's 0.5× downscale would land on a clean 1024. Then three arms:

Arm Pipeline
A downscale 0.5× (Lanczos) → rotate θ (bicubic) → upscale back to 1024
B rotate θ (bicubic) → downscale 0.5× (Lanczos) → upscale back to 1024
ref downscale 0.5× → upscale back to 1024, no rotation at all

Both arms do the same two operations on the same input and produce the same output size. The only variable is order. The ref arm exists so I can separate "how much detail does rotation itself cost" from "how much does the order cost" — without it, an order difference of 0.6 levels could be a rounding artefact of my own code and I would have no way to tell.

Three controls, and I had to fix two of them before they meant anything:

  • θ = 0° must be bit-identical. If the two pipelines agree when nothing is rotated, my arm implementations are equivalent. Measured max difference: 0.0.
  • A flat plane must be order-free. A constant grey image has no structure to reorder, so the interior difference must be exactly zero. Measured: 0.0.
  • rotate(90°) must equal transpose(ROTATE_90). If Pillow's 90° rotation is a bit-exact permutation, right angles are free; if it interpolates, they are not. Measured max difference: 0.0, so it is exact.

Those controls are not decoration. On my first attempt I measured the whole frame and the flat-plane control reported a 0.036 difference on an image that cannot possibly have one — because rotate(expand=False) fills corners with black, and I was averaging the fill. The result was a control leaking into the measurement. I raised the interior margin to 260 px on a 1024 px output and the control went to exactly zero. A control that fails is not a formality; it is telling you your measurement is wrong. If I had skipped it I would have published a number about black corners.

What the numbers say

Rotation HF, downscale→rotate (% of no-rotation ref) HF, rotate→downscale (%) mean |A−B|, grey levels
0° 100.00 100.00 0.000
5° 101.66 103.95 0.597
17° 102.27 103.92 0.604
30° 104.64 106.14 0.636
45° 104.78 108.08 0.672
90° 100.00 99.97 0.132

Left: the order effect is zero at right angles and non-zero everywhere else. Right: both orders exceed the no-rotation baseline — rotation manufactures high-frequency energy

Read the third column first: the order effect is real but small. At 45° — the worst angle — the two orders differ by about 0.67 of a grey level on average across a 1024 px frame. That is not visible as a difference between two images side by side. It becomes visible only after upscaling amplifies it, which is the reason it matters here at all.

Read the last row next, because it is the control that reframes everything else: at 0° and 90° the order difference collapses to essentially zero (0.000 and 0.132 levels). Right angles are lattice-preserving permutations — they move pixels to new addresses without interpolating — so there is nothing for the order to reorder. Every angle in between requires interpolation, and interpolation is what makes order matter. That pattern is what tells you the effect is caused by the rotation and not by my arithmetic.

The result that killed my hypothesis

The second and third columns are the surprise. Both arms carry more measured high-frequency energy than the no-rotation baseline — up to 108% at 45°. If you are reading quickly, that looks like a win. It is not.

Rotation is an interpolation, and any interpolator with negative lobes (Lanczos and bicubic both have them) produces ringing — overshoot near edges. Ringing is high-frequency energy. So a rotated image scores higher on a high-frequency-energy metric while looking softer, because the metric cannot distinguish "detail" from "overshoot about detail". This is the same trap as evaluating an upscaler by PSNR alone: the number rewards a class of artefact.

So the correct reading of the table is not "rotate-then-scale preserves more detail". It is:

  • The order difference is real and reproducible.
  • It is small relative to rotation's own cost, which is the larger term and is paid either way.
  • The direction of the difference on an HF metric is confounded by ringing, so an HF number cannot tell you which order is better — only that they differ.

My prior was "downscale first is better because there is less data to interpolate". The data does not support that, and it does not support the reverse either. What it supports is: the order matters, the magnitude is sub-visual before upscaling, and if you want to know which is better for your content you need a perceptual metric or a human, not a Laplacian. That is a less satisfying answer than the one I predicted, and it is the one I actually have.

The canvas layer, where the difference should have been huge

If your pipeline is in a browser — and a lot of this damage happens in canvas-based share and upload flows — there is a second thing to check, and here the measurement was unambiguous.

Canvas gives you imageSmoothingQuality with three settings: "low", "medium", "high". It reads like a quality dial. The HTML specification's definition makes it a hint with no specified algorithmic effect, but hints usually do something.

I rendered the same 1080×1620 source into a 900×900 canvas at all three settings, at 0° and at 17°, then hashed the resulting ImageData buffers with FNV-1a and compared.

Rotation low medium high
0° hash 3365341244, HF 8.280 hash 3365341244, HF 8.280 hash 3365341244, HF 8.280
17° hash 3797656680, HF 8.410 hash 3797656680, HF 8.410 hash 3797656680, HF 8.410

Byte-identical at every setting. Not "visually similar" — the same FNV hash over 3,240,000 bytes, and the same measured HF energy to four decimals. In Chrome 157 on Linux, imageSmoothingQuality changes nothing about the output. It is a no-op.

That is worth knowing for a concrete reason: if your pipeline leans on "high" to preserve detail through a downscale, you are relying on a property that does not do anything. The kernel is whatever the engine picked, and it is not necessarily Lanczos or even bicubic. You cannot set it, and you cannot detect it from the page. If detail preservation matters, do the expensive resample yourself with a kernel you chose and use the canvas transform only for the final blit at 1:1.

Note also that the 17° render has higher HF (8.410 vs 8.280) than the unrotated one — the same ringing signature as the Python arms. Two independent codebases, two different resamplers, the same direction of artefact. That consistency is what makes me confident the HF-increase is a property of rotation-with-interpolation rather than of Pillow specifically.

Two things to check before you send a file through an upscaler

The practical output of this measurement is small and specific, which is the honest size of the finding.

First: right angles are free, everything else is not. If your geometry work can be expressed as crop, flip, and 90° rotations, do that and stop. The moment you introduce 3° of straightening or a 17° align, you are interpolating, and the order effect and the ringing both switch on together. Crop instead of straighten whenever the framing allows.

Second: do not trust imageSmoothingQuality, and verify with a control. Hash your canvas output at two settings and compare. If the hashes match — as they do in Chrome — you have learned that the dial is inert in your engine, and you can stop counting on it. If they differ, you have learned your engine implements it and you can measure how. Either way you now know something checkable instead of something assumed.

I ran these measurements while evaluating UpgoLAB's browser upscaler, which handles video as well as stills, and the ordering question gets worse in video: a per-frame rotation resamples every frame onto a slightly different lattice, and the inconsistency between frames reads as shimmer even when each frame is fine. Stabilise once, in one pass, before upscaling — never per frame.

The 28-second demo below runs five presets on the product's own output, if you want the spatial half of this before measuring your own files:

The lesson I would carry forward

I set out to confirm a plausible story and produced a set of numbers that contradicted it. The useful part is not the numbers — they are small — but the shape of the mistake:

  • A plausible mechanism (less data to interpolate) is not a measurement.
  • A control that fails is the measurement telling you it is broken, not a formality. Mine did, twice.
  • A metric can move in the opposite direction to quality when ringing is present. HF energy went up while the image got softer.
  • When the direction of a small effect is confounded, the honest output is "it differs, and I cannot rank them with this metric" — not a rank.

If you take one thing from this post, take the third and fourth points, because they apply to every "measure your pipeline" article you will ever read, including mine.

Top comments (0)