Sharpen Before or After Upscaling? The Order Survives Float, Not a File
Every guide to upscaling says the same thing about order: sharpen after you upscale, because sharpening first amplifies detail that is about to be resampled away. I believed the advice and expected to confirm it with a small margin. What the measurements show is more interesting and less tidy. On a float pipeline the two orders are the same operation — matched correctly they agree to 0.002 dB, which is not a difference at all. The advice only starts to be true once something quantises between the two steps. In a real pipeline there is always something: an 8-bit buffer, a JPEG, a PNG. That single step is what the rule is really about, and once you measure it the rule becomes sharper than "do it after" — it becomes a statement about where you are allowed to lose information.
The claim being tested, stated so it can fail
"Sharpen after upscaling" is stated as an absolute. It should therefore produce a measurably better result than "sharpen before upscaling" at comparable settings. The comparison is only fair if the two orders are given the same effective operation, so the first job is to establish what "comparable" means.
A Gaussian blur of radius r applied at 1/F resolution and then upscaled by F is
approximately a Gaussian blur of radius r·F at output resolution — the resampler scales
the blur kernel along with everything else. So the fair test is: sharpen the low-res frame with
radius r, upscale by F, and compare against upscaling first and then sharpening with
radius r·F. Same amount, same effective radius.
Run that comparison in float64, on a 3× restore of a real photograph, and the two orders land almost exactly on top of each other:
| Variant (3×, amount 0.8) | PSNR vs original | HF retained |
|---|---|---|
| Upscale only, no sharpening | 30.288 dB | 5.3% |
| A · sharpen before (r=1.0 low-res, then upscale, float) | 29.489 dB | — |
| B · sharpen after (upscale, then r=3.0 output) | 29.491 dB | — |
0.002 dB. That is not a finding, that is the same number measured twice with different rounding paths. The float experiment says the orders commute. Any advice that treats them as opposites is describing something other than the resampling algebra — and the thing it is describing is the intermediate storage.
What an 8-bit step between them actually costs
A real pipeline cannot keep float64 between the two operations. The low-res frame is a file, or a GPU buffer, or a decoded texture. Insert one round to 8 bits between sharpening and upscaling, and the picture changes:
| Amount | A · 8-bit step, then upscale | B · upscale, then sharpen | A − B | Pixels bit-identical |
|---|---|---|---|---|
| 0.2 | 30.383 dB | 30.420 dB | −0.037 | 83.3% |
| 0.4 | 30.199 dB | 30.276 dB | −0.077 | 82.6% |
| 0.8 | 29.353 dB | 29.491 dB | −0.138 | 80.8% |
| 1.2 | 28.315 dB | 28.471 dB | −0.156 | 79.0% |
| 1.6 | 27.250 dB | 27.399 dB | −0.149 | 77.2% |
The penalty grows with sharpening amount, from 0.037 dB to a plateau near 0.15 dB. It is small in decibels and it is not small in pixels: at amount 0.8, 19.2% of the frame is not the same value in the two results. The mechanism is straightforward once you look for it. Sharpening on the low-res frame pushes 2.1% of pixels outside 0..255, and those pixels are then clamped — the overshoot that a sharpener deliberately creates at an edge is truncated before it ever reaches the resampler. Sharpening at output resolution creates the same overshoot and delivers it, because there is nothing downstream to clamp it.
The rule restated: sharpen after upscaling not because the arithmetic differs — it does not — but because sharpening before the upscale forces the overshoot through a narrower container. The low-res frame has to hold the overshoot and the detail it belongs to, in the same 8 bits.
Where the two orders stop being the same operation at all
A JPEG between the steps is the common case: you have a small source file, you sharpen it, you save it, you upscale the saved file. Measured against the same pipeline with no sharpen at all, and against sharpening at output resolution:
| Quality of the intermediate JPEG | A · sharpen → JPEG → upscale | B · upscale → sharpen | A − B | Intermediate size |
|---|---|---|---|---|
| q70 | 28.943 dB | 29.491 dB | −0.549 | 23,691 B |
| q82 | 29.128 dB | 29.491 dB | −0.363 | 31,144 B |
| q90 | 29.238 dB | 29.491 dB | −0.253 | 42,407 B |
| q95 | 29.296 dB | 29.491 dB | −0.195 | 59,751 B |
Here the penalty is 4× larger than the bare 8-bit step at its worst, and it shrinks slowly with quality — q95 still costs 0.195 dB. This is the shape you would predict from the argument above: the sharpener has created high-frequency energy at the exact spatial frequencies a DCT quantiser is most willing to throw away, and it has done so before the resampler gets to use it. The unsharp mask is, in effect, manufacturing the content that the compressor is most likely to delete.
Note what the last column does and does not say. Raising quality from q70 to q95 recovers 0.354 dB of the 0.549 dB gap while more than doubling the intermediate file. The gap never closes, because the loss is not noise that more bits fix — it is structured detail removed by quantisation of a signal that was artificially boosted in that band.
A negative result I did not expect: the pre-sharpen optimum is not a weak version of the post optimum
The obvious objection to everything above is that I compared the two orders at a matched radius, which is a fair test of the algebra but not a fair test of the practice. Each order should be given its own best radius and amount. So I swept both, six radii × six amounts, for each order independently, and took each one's own maximum.
The result is not the one the earlier framing predicts. The two optima are not the same operation rescaled. Sharpening before the upscale always selects the smallest radius in the grid — 0.5 low-res pixels, which at 3× is 1.5 output pixels. Sharpening after selects between 0.5 and 1.0 output pixels depending on the image. Multiplying the pre-sharpen radius by the scale factor overshoots: the mean ratio of "A's radius × F" to "B's chosen radius" is 1.96× (median 2.00×, range 1.33–2.67×), so the matched pair is roughly a factor of two too blurry at the output side.
Which is a way of saying that the pre-sharpen order is not just slightly worse, it is constrained. It cannot express the sharpening it wants to do, because the smallest useful radius on a 540-pixel-wide frame is already coarser than the sharpening the output needs. The low-res grid is the limit, and no amount of tuning gets around it.
The ordering across 4 distinct sources and 3 scale factors
Each source is content-hashed before use, because an earlier run of this experiment silently picked up two copies of the same JPEG and reported identical numbers twice — which would have inflated the case count by a third. With that fixed, 12 independent cases:
| Source | Scale | Upscale only | A · before (best) | B · after (best) | A − B |
|---|---|---|---|---|---|
| texture | 2× | 33.243 | 33.326 | 33.535 | −0.209 |
| texture | 3× | 30.288 | 30.450 | 30.579 | −0.129 |
| texture | 4× | 28.302 | 28.381 | 28.449 | −0.068 |
| plain | 2× | 33.108 | 33.227 | 33.420 | −0.193 |
| plain | 3× | 29.319 | 29.478 | 29.615 | −0.137 |
| plain | 4× | 27.257 | 27.330 | 27.406 | −0.075 |
| photo | 2× | 34.725 | 34.759 | 34.795 | −0.036 |
| photo | 3× | 33.596 | 33.608 | 33.632 | −0.024 |
| photo | 4× | 32.995 | 33.002 | 33.028 | −0.025 |
| flat sheet | 2× | 31.855 | 31.890 | 32.040 | −0.151 |
| flat sheet | 3× | 29.169 | 29.219 | 29.290 | −0.072 |
| flat sheet | 4× | 27.748 | 27.777 | 27.858 | −0.080 |
B wins 12 of 12. The gap runs from 0.024 to 0.209 dB, mean 0.100 dB, and it shrinks as the scale factor grows in every source — 2× is the worst case, 4× the mildest. That trend is the useful part. At 2× the low-res frame is half-size and its smallest usable radius is still 0.5 low-res pixels, so the pre-sharpen order is most constrained exactly where it has the most to work with. At 4× the frame is already so far from the original that neither order recovers much, and the difference flattens toward zero. The penalty is largest where upscaling is most worth doing.
But the gain is small in absolute terms, and honesty requires saying so
Both orders buy much less than their advocates claim. Averaged over the 12 cases, the best pre-sharpen setting beats doing nothing by +0.070 dB, and the best post-sharpen setting by +0.170 dB. A tenth of a decibel is not a reason to rebuild a pipeline. Everyone's sharpening advice is competing for an improvement that is, on PSNR, close to invisible.
High-frequency energy tells a different and slightly more flattering story, and it is worth including because it shows where the visible difference actually lives:
| 3× restore, grayscale | PSNR | HF energy vs original |
|---|---|---|
| Upscale only | 30.288 dB | 5.3% |
| A · before, at its optimum (r=0.5, amount 0.6) | 30.450 dB | 7.7% |
| B · after, at its optimum (r=1.0, amount 1.0) | 30.579 dB | 9.1% |
Upscaling destroys 94.7% of the high-frequency energy and neither order gets close to restoring it — the best post-sharpen setting recovers about a sixth of what was lost. Its 1.4-point HF advantage over the pre-sharpen optimum is what a viewer would actually see, and it is the reason the rule exists despite the decibel numbers being unimpressive.
The assumption that breaks the tidy story: a non-linear resampler
Everything above used the same resampler. Swap in one that operates in linear light — converting sRGB to linear, resampling, converting back — and the commutation stops holding by an order of magnitude more than the quantisation effect:
| Resampler (3×, amount 0.8) | Sharpening before | Sharpening after | A − B |
|---|---|---|---|
| Linear (resample in sRGB) | 29.489 dB | 29.491 dB | −0.002 |
| Non-linear (resample in linear light) | 26.010 dB | 26.599 dB | −0.589 |
This is the strongest form of the result and the one that survives the "it's just algebra" objection. The float commutation in the first row is a property of linear operators, not a law of image processing. A gamma-space conversion between the two steps is non-linear, so the order matters on its own terms — 0.589 dB, 4.3× the 8-bit effect. If your pipeline resamples in linear light, sharpen after, and don't rely on the commutation argument at all.
The cost side: pre-sharpening is faster, and that is its only advantage
Sharpening before the upscale touches 194,400 pixels instead of 1,749,600 at 3× — a factor of 9.0 in pixel count, because the frame is a ninth the area. On this machine that buys a 1.80× wall-clock speedup:
| Pipeline (1080×1620 output) | Time per frame | Throughput |
|---|---|---|
| Upscale only (baseline) | 118.4 ms | 14.78 MP/s |
| A · sharpen 0.18 MP, then upscale | 119.0 ms | 14.70 MP/s |
| B · upscale, then sharpen 1.75 MP | 214.2 ms | 8.17 MP/s |
Sharpening at output resolution nearly doubles total pipeline time, because the unsharp mask — two separable Gaussian passes plus the blend — costs more than the resampler itself at these sizes. That is a real trade and it is the honest case for pre-sharpening: if you are sharpening a video in a batch job and the difference of 0.1 dB is genuinely invisible to you, 1.80× is a legitimate reason to break the rule. If you are producing a still, it is not.
Where A genuinely wins, which the aggregate number hides
A frame-level PSNR hides the regional structure, and splitting by local gradient shows that the two orders do not fail in the same places. Regions defined on the original's gradient magnitude:
| Region | Share of pixels | A · before | B · after | A − B | Bit-identical |
|---|---|---|---|---|---|
| Flat (lowest 33% gradient) | 19.7% | 34.168 dB | 34.071 dB | +0.097 | 89.9% |
| Mid detail | 70.3% | 34.194 dB | 34.196 dB | −0.002 | 79.7% |
| Edges (top 10% gradient) | 10.0% | 20.894 dB | 21.101 dB | −0.207 | 70.7% |
Pre-sharpening is better on flat regions and worse on edges, and the whole aggregate penalty is produced by the 10% of pixels that are edges. That is a coherent picture rather than a noise pattern: on a flat region there is nothing for the resampler to lose, so clamping the small amount of overshoot costs less than the benefit of sharpening before the interpolation smooths the region out. On an edge, the overshoot is the product, and clamping it before the upscale destroys exactly what the operation was for.
The same data in a different form, the distribution of per-pixel disagreement between the two orders at amount 0.8:
| Percentile of |A − B| | Difference (8-bit levels) |
|---|---|
| p50 | 0 |
| p90 | 1 |
| p99 | 3 |
| p99.9 | 25 |
| max | 83 |
Half the frame is bit-identical and 99% is within 3 levels — so a blanket statement that the orders "differ" is wrong for most of the image. But the top thousandth differs by up to 25 levels and the maximum by 83, and those pixels are on edges. If you have ever compared two sharpening workflows and found them indistinguishable except along one particular contour, this distribution is why.
Colour: sharpen the luma, and the chroma question does not arise
If the unsharp mask is applied to R, G and B independently, each channel gets its own overshoot and the three deltas do not agree, which shifts hue along edges. Applying the mask to luma and adding a single delta to all three channels keeps the delta achromatic by construction. Measured as the standard deviation across channels of the change the sharpener introduces:
| Colour handling (3×, amount 0.8) | Chroma spread of the sharpening delta |
|---|---|
| Unsharp mask on each channel independently | 0.2163 |
| Unsharp mask on luma, delta applied to all channels | 0.1521 |
1.42× less colour error, for one extra multiply-add. This is worth mentioning here because it interacts with the ordering question: a per-channel mask produces more high-frequency chroma, and chroma is the component most likely to be subsampled or quantised later. The pre-sharpen order plus a per-channel mask manufactures the worst possible thing to hand to a 4:2:0 encoder.
Reproducing it
The whole experiment is numpy plus Pillow, no SciPy, because the Gaussian is separable and easy to write. The resampler is a float64 Lanczos-3 so that overshoot is not clipped before it can be measured — using Pillow's uint8 path here silently zeroes the effect, which is how the first version of the step-edge test returned 0.00 overshoot for every variant.
import numpy as np
from PIL import Image
def lanczos3(a, out_hw):
"""Separable Lanczos-3 on float64. Validated against Pillow: mean
error 0.128 levels, p99 0.596, max 1.047."""
...
def gauss(a, sigma):
r = int(np.ceil(3 * sigma))
x = np.arange(-r, r + 1, float)
k = np.exp(-x**2 / (2 * sigma**2)); k /= k.sum()
for axis in (0, 1):
pad = [(0, 0), (0, 0)]; pad[axis] = (r, r)
p = np.pad(a, pad, mode="edge")
n = a.shape[axis]
acc = np.zeros_like(a)
for i, w in enumerate(k):
sl = [slice(None), slice(None)]; sl[axis] = slice(i, i + n)
acc += w * p[tuple(sl)]
a = acc
return a
def usm(a, radius, amount):
return a + amount * (a - gauss(a, radius))
def q8(a):
return np.clip(np.round(a), 0, 255).astype(np.uint8).astype(np.float64)
def psnr(a, b):
return 10 * np.log10(255.0**2 / np.mean((np.clip(a,0,255) - b)**2))
gt = np.asarray(Image.open(src).convert("L"), float)
H, W = gt.shape
F = 3
low = lanczos3(gt, (H//F, W//F)) # the "low-res source"
ctrl = lanczos3(low, (H, W)) # plain upscale
# A: sharpen before, with an 8-bit step in between
A = q8(lanczos3(q8(usm(low, 1.0, 0.8)), (H, W)))
# B: sharpen after, radius scaled by F
B = q8(usm(ctrl, 1.0 * F, 0.8))
print(psnr(A, gt), psnr(B, gt)) # 29.353 29.491
Change q8(...) to a pass-through in the A branch and the two PSNR values converge to
29.489 and 29.491. That single line is the entire result: remove the quantisation and the two orders
become the same operation.
What I would actually do
- Sharpen at output resolution. It wins 12/12, by 0.100 dB on average and 0.209 dB in the worst case (2× scale, textured source), and by 9.1% versus 7.7% on retained high-frequency energy, which is the part a viewer sees.
- If you must sharpen before the upscale — a batch video job where 1.80× matters more than 0.1 dB — use the smallest radius your grid allows and a modest amount. The pre-sharpen optimum sat at radius 0.5 low-res pixels and never wanted more; holding amount at 0.6 and moving the radius from 0.5 to 3.0 costs 2.478 dB in order A, against 1.409 dB for the same radius move in order B.
- Never put a lossy encode between sharpening and upscaling. That is the one combination that costs real quality: 0.549 dB at q70, and still 0.195 dB at q95. If the intermediate must be a file, make it lossless or reorder the pipeline.
- If you resample in linear light, the commutation argument does not apply. The ordering effect there is 0.589 dB on its own, before any quantisation.
- Apply the mask to luma, not to each channel. 1.42× less chroma error for one add, and it keeps the high-frequency chroma away from any later 4:2:0 step.
Two corrections to my own first attempt, kept here on purpose. The step-edge test in the first version returned exactly 0.00 overshoot for all four variants — not because there was none, but because the resampler went through uint8 and clamped it. And the first cross-source run reported three sources when two were byte-identical, which would have inflated the evidence by a third. Both are now asserted against in the script rather than assumed away.
None of this makes sharpening a large lever. On these measurements, the entire ordering question is worth about a tenth of a decibel, and the highest-frequency tenth of the frame is where all of it lives. What is worth knowing is why the rule exists, because that tells you when you are allowed to break it: the orders are algebraically identical, and they only diverge when something between them throws bits away. If nothing does, the rule is advice about a difference that is not there.