Linear Light Upscaling: Better Images, Deeper Shadows
Resumo
Pixel shares seven experiments comparing sRGB and linear-light image resampling using NumPy and Pillow. The findings show why linear light can improve averaging yet cause unexpected shadow loss and affect photographs and unsharp masks, giving the community practical evidence beyond the usual theory.
DEV Community
Linear Light Upscaling Is Correct, and It Cost Me 18 Shadow Levels
Every resampling article opens with the same demo: downscale a black-and-white checkerboard in sRGB, get a muddy grey that should have been mid-grey, then do it in linear light and get the right answer. The demo is correct. I have reproduced it. Then I took that conclusion — "always resample in linear light" — and applied it to a real 512×512 photograph, and to a shadow gradient, and to an unsharp mask. It is not that simple, and the reasons are specific enough to measure. Seven experiments below. Two of them contradict the advice I started with. All of them run on numpy 1.26.4 and Pillow 12.2.0, and every number comes from the snippet at the end. Why the theory says linear light wins sRGB is a non-linear encoding. A stored value of 128 is not 50% of the light — it is about 21%. The encoding exists because human contrast perception is roughly logarithmic, so spending code values where the eye is sensitive wastes fewer bits. Good decision, for storage. Most image operations, though, are physical operations on light. Averaging two pixels means averaging the light they emitted. Average the encoded values instead and you are averaging logarithms, and the average of logarithms is not the logarithm of the average. The error is systematic, not random, so it does not average away at higher resolutions. That argument is airtight, as far as it goes. It is an argument about averaging. An upscaler does more than average, and an 8-bit file has to store the result somewhere. Those two facts are where the tidy story breaks. Method One 512×512 crop from a 1080×1620 photo, converted to 8-bit greyscale so the colour-space question is not confounded by chroma. Two pipelines: sRGB pipeline: resize the 8-bit buffer directly. This is what every naive Image.resize() call does. Linear pipeline: convert to linear float, resize with the same kernel, convert back to sRGB, store 8-bit. Both pipelines use the same filter, because the filter is not the variable being tested. Between the downscale and the upscale each pipeline stores its intermediate as 8-bit, which is what actually happens when a tool writes a temp file or an application holds a texture. High-frequency retention is mean |Laplacian| of the result divided by mean |Laplacian| of the ground truth, both computed in sRGB code values so the two pipelines are compared in the same units the eye sees. Experiment 1: linear wins on detail, loses on PSNR Downscale 4× to 128×128, then back up to 512×512. Kernel PSNR sRGB PSNR linear HF retained sRGB HF retained linear Max error sRGB Max error linear bilinear 25.746 dB 24.763 dB 19.11% 20.23% 153 166 lanczos 27.309 dB 26.123 dB 29.45% 32.97% 151 172 Two things point in opposite directions, and both are real. Linear light keeps more high-frequency detail: +1.12 percentage points at bilinear, +3.52 at lanczos, a 12% relative improvement. That is the theoretical prediction confirmed — resampling in the correct space preserves structure better. Linear light also has worse PSNR, by 0.98 dB at bilinear and 1.19 dB at lanczos. And worse worst-case error: 172 code values against 151. The resolution is that PSNR is a mean squared error over the whole image, and the linear pipeline spends its error where the eye is most sensitive: the shadows. MSE does not know that a 6-code-value error at value 12 is far more visible than a 20-code-value error at value 200. It weights them by the same squared distance. So which pipeline is "better" depends on whether you believe your metric or your optics. That is not a satisfying answer, so I went looking for where the error actually lives. Experiment 2: the metric and the pipeline disagree by brightness Same round trip, lanczos, absolute error bucketed by the brightness of the source pixel. Source brightness Pixels MAE sRGB MAE linear Winner 0–32 23,513 6.216 11.085 sRGB, by 78% 32–64 9,676 13.185 18.867 sRGB, by 43% 64–96 16,419 9.596 12.229 sRGB, by 27% 96–128 19,729 7.736 7.875 sRGB, marginally 128–160 31,386 5.375 5.023...

Powered by Readybot.io