DEV Community

Jay
Jay

Posted on

Five resampling filters on one product photo: 5.4x on edge error, and sharpening makes it worse

Six posts in this series have pointed measurement at cutout edges: the original geometry check, colour contamination, the edge-hue test, its blind spot, a coverage matrix, and seven rembg models. All of them are about the matte.

This one is about what happens to the pixels after the matte, in the step nobody chooses. Image.resize() defaults to BICUBIC. cv2.resize() defaults to INTER_LINEAR. Most export pipelines call one of them with no filter argument, once per marketplace preset, and the person who signed off on the pipeline has never seen the difference.

So I measured it. Same photo as the rest of the series, five PIL filters, three reduction ratios, one sharpening sweep, and an enlargement case. The result is not "use LANCZOS". It's that the filter is worth 5.4x on edge error while looking like nothing at all on the whole-frame average — and that sharpening, the tool everyone reaches for after a resize, moves the edge in the direction that looks right and the error in the direction that hurts.

The setup

One image, unchanged from the series: a 1200×800 Unsplash photo of a white ceramic mug. One edge: the left silhouette, where the mug's body at luminance 206 meets a background at 254. That's a 48-level step, vertical over rows 470–580, sitting at x ≈ 152.

That edge is the easiest thing I could have chosen. High contrast, nothing behind it, straight for 110 rows. Whatever the filters cost here, they cost more on a soft shadow or a rounded shoulder. Treat the numbers as a floor.

Three instruments, and each one gets checked against a case with a known answer before it is allowed to say anything:

  • rise — the 10–90% distance across the edge, in output pixels, by linear interpolation between the two samples that bracket each level. On a Gaussian-blurred step this equals 2.563σ, so synthetic blurs of known σ are a ground truth.
  • overshoot — the worst excursion past the plateaus, as a fraction of the step height. This is ringing: the halo a viewer reads as "sharp". Plateaus are sampled from the outer fifth of a window twice as wide as the rise window, so the ringing cannot poison the level it is measured against.
  • RMSE — distance from the master, over the whole frame and over edge pixels only (2,114 pixels where |∇L| > 8 in the master).

The rise check, on synthetic steps:

σ 2.563σ expected (quadrature) measured error
0.5 1.28 1.51 1.30 13.9%
1.0 2.56 2.68 2.67 0.7%
1.5 3.84 3.93 4.05 3.1%
2.0 5.13 5.19 5.17 0.4%
3.0 7.69 7.73 7.91 2.3%

"Expected" is the generator's own 0.80 px floor combined with 2.563σ in quadrature, because the synthetic step is not infinitely sharp. Worst error is 13.9%, at the smallest blur where the floor dominates. That is the error bar for everything below: differences smaller than about 15% of a rise measurement are not differences. Overshoot on the same synthetic steps reads 0.0% at every σ, which is the other half of the check — a clean step has no halo, and the instrument agrees.

The master itself: rise 4.07 px, overshoot 4.3%. That 4.3% is the photo's own edge, produced by whatever the camera and the JPEG encoder did, and it sets a baseline that no filter below should be praised for beating.

Roundtrip: down, then back up

A downscale has no ground truth at the smaller size — nothing to compare against. So each case here goes master → 1/s → back to 1200 px, and is scored against the master. What comes back is not the master; the difference is what the filter destroyed, measured at a scale where the comparison is fair.

ratio filter RMSE edge RMSE rise overshoot
1/2 NEAREST 1.23 12.80 4.33 4.3%
1/2 BOX 0.92 8.34 4.42 4.3%
1/2 BILINEAR 0.80 5.75 5.25 4.3%
1/2 BICUBIC 0.56 3.35 4.32 4.3%
1/2 LANCZOS 0.51 2.38 4.03 4.3%
1/4 NEAREST 2.01 19.33 4.62 4.1%
1/4 BOX 1.75 14.61 4.66 4.2%
1/4 BILINEAR 1.51 10.82 7.17 4.2%
1/4 BICUBIC 1.17 8.06 5.87 6.2%
1/4 LANCZOS 1.06 6.86 5.27 8.3%
1/8 NEAREST 3.23 26.23 7.89 4.0%
1/8 BOX 2.88 19.84 8.42 3.2%
1/8 BILINEAR 2.69 17.70 12.84 3.1%
1/8 BICUBIC 2.26 14.38 9.73 6.2%
1/8 LANCZOS 2.11 12.95 8.27 8.4%

Four things in that table are worth more than the ranking.

The ranking never moves. NEAREST, BOX, BILINEAR, BICUBIC, LANCZOS, worst to best, at all three ratios. If you pick a filter once you can stop thinking about it, and the pick is LANCZOS.

The spread collapses with the ratio, and that is not good news. At 1/2, edge error runs 12.80 to 2.38 — 5.4x. At 1/8 it runs 26.23 to 12.95 — 2.0x. The filters converge because every one of them has lost the same detail by then; they are all equally wrong. A small table of test images resized to thumbnails will make any two filters look similar, for this reason.

Whole-frame RMSE is nearly blind here. At 1/2, NEAREST scores 1.23 out of 255, which is 0.5%. If your quality gate averages over the frame, every filter in this table passes, including the one that turns a straight edge into a staircase. The edge is 2,114 pixels out of 960,000 — 0.2% of the image — and it is the part anyone looking at a product photo sees first. Restricting to those pixels changes the spread from 2.4x to 5.4x.

Ringing arrives with the ratio, not with the filter. At 1/2 no filter exceeds the master's own 4.3%, including LANCZOS, which has negative lobes and therefore can ring. At 1/4 it reads 8.3%, at 1/8 8.4%. NEAREST stays at 4.0% all the way down, because a box footprint never overshoots — it can't ring, it can only staircase. So "LANCZOS rings" is true only past a certain ratio, and "NEAREST is safe" is true only if you don't care about the staircase.

There is a contradiction buried in the 1/8 block. LANCZOS has the best RMSE by 2x and a wider 10–90% edge than NEAREST — 8.27 px against 7.89. Both are correct. RMSE sums a slight halo against a lot of staircase and prefers the halo. The rise measures one property and sees only the ramp. Neither number is the answer; a real acceptance test would need both, and most don't have either.

Sharpening buys sharpness with fidelity

ImageFilter.UnsharpMask(radius, percent, threshold) subtracts a blurred copy of the image from itself. It does not know what an edge is. Applied to the 1/2 LANCZOS roundtrip — the best case in the table above — ten settings:

radius amount RMSE edge RMSE rise overshoot
— none 0.51 2.38 4.03 4.3%
1.0 50% 0.52 2.31 3.86 4.3%
1.0 100% 0.60 3.06 3.52 4.3%
1.0 150% 0.69 4.01 3.39 5.8%
1.5 50% 0.59 2.94 3.44 5.2%
1.5 100% 0.83 5.32 2.88 6.2%
1.5 150% 1.08 7.75 2.62 7.7%
2.0 50% 0.70 3.90 3.12 6.2%
2.0 100% 1.11 7.68 2.53 10.6%
2.0 150% 1.51 11.28 2.23 14.9%

The last row takes 45% off the rise and triples the whole-frame error. Edge error goes from 2.38 to 11.28 — 4.7x — and overshoot from 4.3% to 14.9%, which is 7 luminance levels of halo on a 48-level step. Sharpening did not restore anything the filter destroyed. It moved the 10–90% crossings closer together by bending the curve, and the bending is the error.

The first row of the sweep is the interesting one, because it's free: radius 1 at 50% changes edge RMSE from 2.38 to 2.31 and whole-frame RMSE from 0.51 to 0.52. Both are inside the instrument's noise. A light pass at a small radius is not a trade, and if that is what your pipeline does, this post is not about you. The post is about the slider that goes to 150%.

The same edge after a 1/8 roundtrip through five filters, at 5x, with edge error at three ratios

Sharpen before the resize, or after?

Four ways to put the same sharpening in the same 1/2 LANCZOS pipeline. Radius 1.5 at 100%, and one variant where the radius is scaled to match:

where RMSE edge RMSE rise overshoot
sharpen the master, then resize 0.79 5.13 2.91 6.0%
resize, then sharpen 0.83 5.32 2.88 6.2%
resize, sharpen at half size, radius 1.5 1.54 11.15 2.34 17.0%
resize, sharpen at half size, radius 0.75 0.78 4.93 2.91 6.2%

The first two are a tie. Order, on its own, does not matter. What matters is that radius is in output pixels: the same 1.5 at half resolution covers twice the image distance once you scale back up, which is why the third row is a different, much worse operation than the second, and why the fourth row — same effect, correct number — comes back to the tie.

That's the trap worth remembering. If you chain resize and sharpen, either sharpen at output size, or scale the radius by the ratio. Keeping the number and changing the resolution is not the same filter.

Luminance across the edge for the master, the 1/2 LANCZOS roundtrip and the sharpened version, plus ten sharpening settings on a rise-vs-error plane

Enlargement: nothing is recovered, and one thing is faked

The other direction, because half of these pipelines exist to hit a 2000×2000 minimum from whatever the seller uploaded. A 600×400 export of the master, blown up to 2000×1333 with each filter:

filter rise gradient energy
NEAREST 6.57 0.1541
BOX 6.57 0.1541
BILINEAR 8.00 0.1513
BICUBIC 7.25 0.1577
LANCZOS 6.82 0.1639
LANCZOS + unsharp r2 150% 4.92 0.1740

BOX and NEAREST come out within 0.05 px of each other on every edge number in the table, which is not a coincidence: above a ratio of 1, an output pixel's box footprint almost never spans two source pixels, so there is almost nothing to average. They are not literally the same image — 0.055% of pixels differ, by up to 59 levels where a box lands on a sliver of the neighbouring pixel — but on a photo, the difference is invisible. If you have both in a filter switch, one of them is dead code.

BILINEAR is 1.2 px wider than LANCZOS and has lower gradient energy: that is blur the filter added. Blur is honest — it doesn't claim to be detail.

The last row is not. The photo's own edge is 4.07 px at 1200 px wide, so at 2000 px it would be 6.79 px; LANCZOS lands at 6.82, which is as close to the floor as this instrument can resolve. Sharpening a 600 px file pushed to 2000 px takes it to 4.92 px, 27% narrower than the original photo's edge. No filter can make a copy sharper than the thing it copied. A 10–90% rise measurement cannot tell that it happened — it just returns a smaller number, which is exactly what a reviewer would call "sharper".

One more detail from the same row: radius 1 at 100% on that enlargement leaves the rise at 6.82, unchanged, while raising gradient energy from 0.1639 to 0.1646. A radius has to approach the width of the ramp before it bends it. On a 3.33x enlargement the ramp is ~7 px, and a 1 px radius is a rounding error on it.

A 600 px source enlarged to 2000 px, before and after sharpening, with the rise of six enlargement variants against the photo's own edge

What I'd put in the pipeline

  • One filter, everywhere. LANCZOS, and stop overriding it. It wins at every ratio in the table, and the alternative in most codebases is the library default rather than a decision.
  • Chaining is a tie only if your filter is good. I measured master → 1/8 in one pass against master → 1/2 → 1/4 → 1/8. LANCZOS: 12.95 edge RMSE direct, 12.90 chained — a tie. NEAREST: 26.23 direct, 32.55 chained, 24% worse. The "resize in steps" advice exists because cheap filters have small support; give it a filter with proper support and the extra passes buy nothing but time.
  • Keep each reduction at 4x or less. At 1/2 nothing rings; at 1/4 LANCZOS is already at 8.3% and at 1/8 it stays there. A 3000 px master exported to 2000×2000 is a 1.5x, nowhere near the region where any of this matters.
  • Sharpen at output size, at radius 1, at 50–100%, if you sharpen at all. Past that, the table says you're buying something.
  • Never sharpen an upscale. It cannot add detail, and it narrows the ramp below what the source supports. The instrument that a QA step would use cannot see the difference.
  • Score edge pixels, not the frame. A 1.23 RMSE average passes a staircase.

The measurement is small enough to keep as a test. This is the whole rise instrument, minus the interpolation helper:

import numpy as np
from PIL import Image

def rise_px(img, rows, win):
    a = np.array(img.convert("L"), dtype=float)
    vals = []
    for y in range(*rows):
        seg = a[y, win[0]:win[1]]
        pad = max(int(len(seg) * 0.15), 3)
        lo, hi = np.median(seg[:pad]), np.median(seg[-pad:])
        if hi - lo < 20:
            continue
        x10 = cross(seg, lo + 0.1 * (hi - lo))
        x90 = cross(seg, lo + 0.9 * (hi - lo))
        if x10 is not None and x90 is not None and x90 > x10:
            vals.append(x90 - x10)
    return float(np.median(vals))
Enter fullscreen mode Exit fullscreen mode

Point it at a ten-row strip across any known edge in a test fixture, and you have a resize regression test that catches a filter change, a library upgrade that moves a default, or somebody's sharpening pass — which is how I found most of this.

What this doesn't cover

One photo, one edge, one polarity. A dark subject on a light background. A light subject on dark would flip the sign of the ringing and, at these magnitudes, could look different to a viewer even where the numbers match.

A JPEG master that already rings. The 4.3% baseline overshoot is the photo's, not the filters'. A clean PNG master would show filter ringing more clearly, especially at 1/2 where everything here sits at the baseline.

A roundtrip is not a downscale. Going down and back up costs each filter twice and can flatter the ones that ring, because the upscale re-smooths the halo. A single downscale, scored at the small size against a downsampled master, would give different absolute numbers — probably smaller gaps, same order.

Luminance only. Resizing also desaturates edge pixels, and the series has the chroma instruments to test that; I didn't run them here. A colour edge would not necessarily behave like a grey one.

sRGB, not linear. PIL resizes in the file's own encoding, which for a product photo is sRGB, so the filtering happens on gamma-encoded values. Doing it in linear light changes both the edge brightness and the ringing amplitude, and that is its own measurement, which I have not run. This post is about what the libraries do, not what they ought to do.

The rise instrument is good to ~15%. Don't quote the difference between 2.31 and 2.38 as a finding. I did, once, and had to walk it back.

I work on PicViva, an editor whose resize for marketplaces exists to hit five platforms' size rules from one master, and whose clarity enhancement is the sharpening pass described above with the slider exposed. The reason this got measured instead of guessed is that the two features disagree: the resize wants to be gentle and the sharpen wants to be visible, and shipping both with per-marketplace presets means I had to know which one was allowed to move first — and what to do when a seller turns the sharpening up because the exported thumbnail "looks soft".

If you've measured this on your own photos: does LANCZOS come out ahead for you too, or does your subject break the ranking?

Top comments (1)

Collapse
 
fireflyteamapps profile image
Firefly Team •

One case worth adding to the matrix: Image.thumbnail() defaults to reducing_gap=2.0, so it does a cheap integer box reduce before the LANCZOS pass, and a pipeline that calls thumbnail() instead of resize() at 1/8 may not be getting the 12.95 you measured. I'd be curious whether it lands nearer your direct LANCZOS number or drifts toward the BOX row.