Your Upscaled JPEG Scores 62 dB Against a Buffer It Invented
DEV Community

Your Upscaled JPEG Scores 62 dB Against a Buffer It Invented

The two yardsticks, and why only one of them is about your picture

A 4× upscale saved at quality 100 scored 62,21 dB against the buffer it came from. The same file, same bytes, same 106,9 KB, scored 46,58 dB against the photograph it was made from.

Nothing about the JPEG changed between those two measurements. The only variable was the picture I held up as the reference, and moving it cost 15,63 dB.

That gap is the whole subject of this article, and it explains a class of advice you have probably read: "upscale first, then export at high quality, because the file is big anyway." The byte saving is real. The quality claim attached to it usually is not.

When you save a JPEG and then measure PSNR, the obvious reference is the buffer you handed to the encoder:

PSNR( buffer_before_save , pixels_after_save )

Call this the arm-local score. It answers one question: how faithfully did the encoder reproduce what I gave it? That is a legitimate engineering question. It is also, on its own, completely blind to whether the thing you gave the encoder was any good.

The other yardstick is the picture as it existed before you started mangling it:

PSNR( native_crop , delivery_after_upscale_and_save )

Call this the truth score. It answers: does the reader end up with more of the real scene?

My 62,21 dB file was a real 512-pixel crop upscaled 4× to 2048 and saved at q100. The encoder did an excellent job on the buffer it was handed. The buffer was a reconstruction, and the reconstruction was not the photograph.

The method, so you can attack it

Seven photographs, one 512-pixel centre crop each, taken from the campaign's data/images/ set - all real photos, no synthetic charts, no test patterns. Pillow and numpy.

The truth for every measurement is the native 512-pixel crop. That is the best detail actually available for that patch of scene, and it is the same image in every arm.

There are two experiments, and they answer different halves of the question.

Experiment 1 - the scoreboard question. Take the native 512 crop, upscale it to 4× (2048 px) with Lanczos, save that buffer at quality q, then block-average the result back down to 512 so it can be compared with the truth at all. This is the file whose arm-local score is 62,21 dB at q100.

The block-averaging is necessary and it is also a caveat: an f×f block of the 2048 field covers exactly one pixel of the 512 truth, so averaging is the only like-for-like comparison. It is not the Laplacian of a re-downsampled image; it is per-native-pixel curvature.

Experiment 2 - the delivery question. Build a 512-pixel delivery from a smaller source: Lanczos down to the target size, Lanczos back up to 512, save at q. Three arms:

  • native 512 saved directly at quality q (no resize at all)
  • 2× from 256: Lanczos down to 256, Lanczos back up to 512, saved at q
  • 4× from 128: Lanczos down to 128, Lanczos back up to 512, saved at q

The downscale is how I create the low-resolution source. It is a deliberate simulation, and I will be honest about what it can and cannot model at the end.

Experiment 2 is the one that produces the money table, because all three arms deliver the same 512×512 output and differ only in how much detail they had to work with.

For the structural half of the analysis I use the Laplacian field of the luma plane - a 4-neighbour second difference, the same operator as nhip115_jpeg_F.py. It responds to curvature, so it is sensitive to exactly the detail an upscale is supposed to add.

Seven controls, all passing on the run that produced every number below:

  • K1: this run's 1× save PSNR matches the stored nhip115_jpeg_E.json - 45,93 dB = 45,93 dB
  • K2: this run's 1× keep_pct matches the same file - 102,79 % = 102,79 %
  • K3: the encoder is deterministic on this machine - identical bytes twice
  • K4: the round trip at factor 1 is the identity - true
  • K5: a flat 128 field at q100 survives exactly - max deviation 0
  • K6: no saved arm beats its own resampling round trip - all overshoots ≤ 0
  • K7: the native arm's r² matches nhip115_jpeg_H.json at all five q - 5/5 exact

K6 is the one worth staring at. Adding a lossy step to a pipeline cannot increase fidelity to the original. If any arm had scored above its encoder-free round trip, the measurement would be broken. None did - the largest overshoot was −0,003 dB.

The scoreboard climbs 12,80 dB. The truth climbs 0,85 dB.

Experiment 1, at 4×, saved at five quality settings, measured both ways:

  • q 70: r² vs its own buffer 0,2920, r² vs the truth 0,0117, PSNR vs its own buffer 49,41 dB, PSNR vs the truth 45,731 dB
  • q 85: r² vs its own buffer 0,3752, r² vs the truth 0,0115, PSNR vs its own buffer 52,53 dB, PSNR vs the truth 46,307 dB
  • q 90: r² vs its own buffer 0,4320, r² vs the truth 0,0113, PSNR vs its own buffer 54,26 dB, PSNR vs the truth 46,468 dB
  • q 95: r² vs its own buffer 0,5384, r² vs the truth 0,0113, PSNR vs its own buffer 56,59 dB, PSNR vs the truth 46,531 dB
  • q 100: r² vs its own buffer 0,8378, r² vs the truth 0,0115, PSNR vs its own buffer 62,21 dB, PSNR vs the truth 46,578 dB

Read the PSNR columns together. Going from q70 to q100:

  • the scoreboard rises 12,80 dB - a transformation, the kind of number that makes people say "wow, q100 is so much better"
  • the truth rises 0,847 dB - three quarters of a dB, invisible
  • the reported gap between the two widens from 3,68 dB to 15,63 dB

And the r² column is worse than flat. Against its own buffer the structure correlation climbs from 0,2920 to 0,8378, a 187 % relative improvement, because the encoder is now faithfully reproducing the smoothing artefacts it produced at low q. Against the truth it goes 0,0117 → 0,0115. It drifts down.

At q100 you are paying five times the bitrate to reproduce, with exquisite precision, a structure that was already 98,9 % gone.

The sharpest version of that statement needs Experiment 2, because the file whose arm-local r² is highest is the 512-delivery built from 128: the same 4× q100 file, 106,9 KB, r² measured against the buffer the encoder was handed (arm-local) 0,8979, measured against the native crop it was made from (truth) 0,0286.

Identical bytes, identical 106,9 KB to the tenth of a KB. A 31× difference in reported structural fidelity, decided entirely by which picture you hold up as the reference. Not one parameter of the encoder changed between those two lines.

Why: the save keeps 0,43 of a signal that is only 0,01 real

The r² numbers above are not a paradox once you separate the two things that happen to a pixel on its way to the reader:

  • 1×: r² truth→upscale 1,0000, r² upscale→save 0,9122, r² truth→save 0,9122, save PSNR (arm-local) 45,93 dB
  • 2×: r² truth→upscale 0,2164, r² upscale→save 0,8356, r² truth→save 0,2114, save PSNR (arm-local) 51,27 dB
  • 3×: r² truth→upscale 0,0244, r² upscale→save 0,6688, r² truth→save 0,0235, save PSNR (arm-local) 53,33 dB
  • 4×: r² truth→upscale 0,0116, r² upscale→save 0,4320, r² truth→save 0,0113, save PSNR (arm-local) 54,26 dB

All saves at q90.

At 1× the resize column is 1,0000, because the "resize" is the identity. The save is the only thing that touches the file, so it takes the whole 0,088 of loss and everything is coherent.

At 4× the columns split apart. The resize round trip destroys structure down to 0,0116. The save then takes that wreckage and preserves 0,4320 of it. The reader receives 0,0113.

The save's own honesty - 43 % of the structure survived my encoder - is a percentage of almost nothing. It reads, in a report, as a mid-range result. It is a mid-range result on a quantity whose denominator has already been voided.

The keep_pct column of the energy measurement shows the same trap from a different angle. Absolute high-frequency energy after a q90 save, as a percentage of before:

  • 1×: HF before save 6,108, HF after save 6,279, keep 102,79 %
  • 2×: HF before save 2,301, HF after save 2,472, keep 107,48 %
  • 3×: HF before save 1,314, HF after save 1,523, keep 116,01 %
  • 4×: HF before save 1,021, HF after save 1,149, keep 112,53 %

Every number is over 100 %. The save added high-frequency energy at every single scale. That is not detail: it is quantization noise, which is curvature by definition, summed into a metric that cannot tell curvature from structure.

The percentage even rises with the factor - 102,79 % at 1× to 116,01 % at 3× - because the signal being "preserved" is smaller, so the same amount of injected noise is a larger fraction of it. A metric that goes up when you damage the picture more comprehensively is not measuring the picture.

nhip115_jpeg_F.py was written because I did not believe the 102,79 %. It separates the two signals by correlation rather than magnitude, and it confirms the diagnosis: at 1× the save's gain is 0,9609 and the orthogonal residual is 6,017; at 4× the gain falls to 0,7267 while the residual stays substantial. The energy sum was never detail.

The flat-field control (F2) settles it: a uniform 128 field saved at q75, q90 and q100 comes back with one distinct value, min = max = 128. There is no curvature to preserve in a flat field, so none appears. Everything measured over 100 % on real photos came from the encoder, not the scene.

The JPEG is innocent. The resize already spent it.

If the save were the problem, the fix would be obvious: raise q, or drop JPEG. Neither works, and the reason is that the bill was paid before the encoder saw the file.

Measure the resampling round trip with no encoder at all - downsample to the target size and upsample back, then compare to the truth:

  • native 512: resize round trip alone 99,00 dB (identity), same arm, best possible JPEG (q100) 58,86 dB, what JPEG costs 40,14 dB
  • 2× from 256: resize round trip alone 35,567 dB, same arm, best possible JPEG (q100) 35,554 dB, what JPEG costs 0,013 dB
  • 4× from 128: resize round trip alone 29,530 dB, same arm, best possible JPEG (q100) 29,527 dB, what JPEG costs 0,003 dB

That is the answer, and it is not the one I expected. At 2× and 4×, the encoder costs one to three hundredths of a decibel. Avalanche, chroma subsampling, rounding, DCT - all of it, together, in seven photos, costs 0,003 dB.

The 29,53 dB you get is 29,53 dB before compression is applied. The arm is capped by its own resampling. No quality setting, no codec, no byte count can move it, because the information was discarded by the downscale and the upscale is reconstructing a plausible smooth field rather than recovering the original.

When I checked the ceiling explicitly - the best PSNR each arm achieves at any quality from q70 to q100 - the picture is the same:

  • native 512: best truth-PSNR (its ceiling) 58,86 dB at q 100, size there 135,6 KB, cheapest q 70 size there 24,3 KB (40,76 dB)
  • 2× from 256: best truth-PSNR (its ceiling) 35,55 dB at q 100, size there 122,2 KB, cheapest q 70 size there 22,7 KB (34,96 dB)
  • 4× from 128: best truth-PSNR (its ceiling) 29,53 dB at q 100, size there 106,9 KB, cheapest q 70 size there 20,0 KB (29,46 dB)

Look at the last two rows. For the 4× arm, the entire q70-to-q100 range buys 0,07 dB (29,46 → 29,53). For the 2× arm, 0,59 dB (34,96 → 35,55). For the native arm, 18,10 dB (40,76 → 58,86).

Compression headroom exists only where there is real detail for the encoder to spend bits on. On an upscaled buffer there is no such detail, so the quality slider is a volume knob wired to nothing.

The money question, at one honest yardstick

Same fidelity yardstick - PSNR against the native truth - for three deliveries:

  • native 512, q70: 24,3 KB, 40,76 dB, r² 0,7578
  • native 512, q90: 44,5 KB, 45,93 dB, r² 0,9122
  • native 512, q100: 135,6 KB, 58,86 dB, r² 0,9958
  • 2× from 256, q100: 122,2 KB, 35,55 dB, r² 0,2551
  • 4× from 128, q100: 106,9 KB, 29,53 dB, r² 0,0286

The cheapest native save in the table - q70, 24,3 KB, the setting most people would call "good enough for the web" - beats the most expensive 4× save by 11,23 dB, at 4,40× fewer bytes, with 26× the structural correlation.

There is no quality setting at which the upscale catches up, because there is no quality setting that addresses the actual loss.

The r²-per-kilobyte column of nhip115_jpeg_H.json makes the incentive explicit. The native arm falls from 0,03114 r²/KB at q70 to 0,00734 at q100 - diminishing returns, as you would expect. The 4× arm falls from 0,02102 to 0,00840. Both lose efficiency as quality rises.

But the 4× arm's starting efficiency is worse than the native arm's ending efficiency, and it never recovers. You are buying a worse product at a worse rate.

Quality 100 is not lossless, and that is a separate bill

Everything above is about the yardstick. This part is about q100 itself, because the "just save it at maximum" advice deserves its own audit. Save the same seven crops at q95, q100 and q100 with 4:4:4 chroma and compare to the pre-save buffer:

  • q95, default 4:2:0: mean absolute error 0,8386 code levels, max error 34, mean size 64,9 KB, PSNR 44,974 dB
  • q100, default 4:2:0: mean absolute error 0,2587, max error 28, mean size 135,6 KB, PSNR 49,249 dB
  • q100, 4:4:4: mean absolute error 0,1281, max error 4, mean size 183,1 KB, PSNR 56,164 dB

Quality 100 is not lossless. It rounds DCT coefficients instead of quantizing them flat, but it still rounds, and 24,82 % of pixels come back changed.

A mean error of a quarter of a code level sounds negligible until you remember that it is applied to every pixel in a file you have already paid a generation for. And the max error of 28 at q100 is only 6 less than the max error of 34 at q95 - the tail does not disappear, it thins.

The chroma half is worth knowing separately: switching to 4:4:4 at q100 cuts max error from 28 to 4 and buys 6,9 dB, at 35 % more bytes. If you have a file with saturated colour on a hard edge - signage, product colour, a red logo on white - that is the setting that does something, and it has nothing to do with upscaling.

Progressive JPEG: a free 10 %, until you upscale

nhip115_jpeg.json also settles a related question. Progressive and baseline JPEG at the same quality are pixel-identical - confirmed across the ladder and at every scale factor in F3 - and progressive is smaller:

  • q 70: baseline 24,3 KB, progressive 24,0 KB, saved 1,45 %, scans 1 / 10, pixels identical yes
  • q 80: baseline 30,2 KB, progressive 29,7 KB, saved 1,81 %, scans 1 / 10, pixels identical yes
  • q 85: baseline 35,3 KB, progressive 34,3 KB, saved 2,58 %, scans 1 / 10, pixels identical yes
  • q 90: baseline 44,5 KB, progressive 42,6 KB, saved 4,26 %, scans 1 / 10, pixels identical yes
  • q 95: baseline 64,9 KB, progressive 60,3 KB, saved 7,10 %, scans 1 / 10, pixels identical yes
  • q 100: baseline 135,6 KB, progressive 121,8 KB, saved 10,16 %, scans 1 / 10, pixels identical yes

A free 10 % at q100, zero quality change, and the progressive scan structure lets a browser paint something useful before the last scan arrives. There is no argument against it.

But it does not compose with upscaling. Re-run the same comparison on the resized buffers, at q90:

  • 1×: baseline 44,5 KB, progressive 42,6 KB, saved 4,26 %
  • 2×: baseline 113,7 KB, progressive 110,5 KB, saved 2,85 %
  • 4×: baseline 295,0 KB, progressive 286,3 KB, saved 2,96 %

The saving drops from 4,26 % to under 3 % once the image has been resized. The bytes-per-pixel column explains why: 0,1736 at native falls to 0,0720 at 4×. Upscaled buffers are already compressible in the trivial sense - they contain little genuine high-frequency content - so there is less for progressive's s

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.