DEV Community

Jay
Jay

Posted on

Four ways to extend a product photo, scored against the pixels that were deleted

Seven posts in this series have measured the edge of a cutout: the original geometry check, colour contamination, the edge-hue test, its blind spot, a coverage matrix, seven rembg models, and what resampling does to it. All of those are about pixels that exist. This one is about pixels that don't.

Extending a frame is the one operation in this trade with no reference. A cutout can be scored against the original photo. A resize can be scored against the master. An outpainted band has nothing behind it — the content that ought to be there was never captured — so it gets judged by eye, and by eye it usually passes, because a fill only has to be plausible for the half second someone looks at a listing.

That's not a measurement problem you can solve. It is a measurement problem you can bound, and the bound is simple: take a photo that does have the content, hide it, and ask the method to put it back. Every number below comes from that. The fills never see a blank canvas; they see a photo with a strip cut out of it, and they are scored on the strip that was actually there.

It is a generous test. Real outpainting asks a method to invent a plausible continuation; this asks it to reproduce one specific continuation, which is harder and more falsifiable. If a method can't clear this bar, the bar isn't the problem.

How much is missing

The geometry comes first, because it sets the scale of everything after.

A square export extended to a taller frame needs h extra rows, where h / W is the aspect change:

source target band height ratio
1:1 4:5 0.25 W Instagram feed
1:1 2:3 0.50 W Pinterest, some PDPs
1:1 9:16 0.78 W Stories, Reels, TikTok

At 4:5 you are inventing a quarter of the width in height. At 9:16 you are inventing more than three quarters of the original frame. Those are different problems, so I ran all three.

The instruments, and what each one is for

Five numbers per fill. Each is calibrated on a case with a known answer before it is allowed to say anything about a photo.

RMSE against the deleted band. The only metric here with ground truth, and the only one that can distinguish "plausible" from "right". Median over three channels, 0–255.

Seam. The row-to-row luminance jump at the join, divided by the same statistic on the rows just below the join. Dividing by the whole-frame median was my first attempt and it is wrong: it scored the true continuation at 0.65, because any photo with a busy product averages more detail than its backdrop does, so the seam looked calm next to it. The reference has to be local. With a local reference the true continuation scores 1.02, which is the calibration.

Curvature. The second difference at the join, normalised the same way. This is the instrument that catches what the first difference misses: a fill can match the level exactly and still break the slope, and the eye reads that break as a crease.

Texture. High-frequency energy (standard deviation of the band minus a σ=3 blur of itself) relative to the band it replaced. 1.0 means the fill is as detailed as the real content. It says nothing about whether the detail is right.

Reflection score. Pearson r between the added band and the vertically flipped rows beneath it, at every plausible band height, computed on the vertical first difference rather than on the rows themselves. The difference matters: a photo shot against a lighting falloff correlates with its own reflection at r ≈ 0.93, so the naive version flags every studio backdrop — including the calibration row, which by construction is not mirrored. Differencing removes the slow trend and leaves true reflections exact, because if p[i] = s[h-1-i] then dp[i] = ds[i].

On synthetic data with a known answer:

row RMSE seam curvature reflection
the real band (calibration) 0.00 1.02 1.07 0.069
reflected 29.34 1.04 1.22 1.000
clamped 16.09 0.00 0.61 —
clamped + blurred 16.25 1.07 1.03 0.002
stretched 22.64 1.15 1.38 0.022

The real band scores seam 1.02 and reflection 0.069; the reflected band scores exactly 1.000. That's the whole validation: an instrument that can't tell a real continuation from its own reflection is measuring noise.

Five fills, each about six lines

All five take the core image and the band height and return the extended frame. Nothing here needs a model, and that's the point of including them — these are the fills that get written by hand in an afternoon, and they are what your "just extend the canvas" shortcut actually does.

def mirror(core, band):            # reflect about the seam, no row repeated
    return np.vstack([core[band:0:-1], core])

def smear(core, band):             # clamp: repeat the top row
    return np.vstack([np.repeat(core[:1], band, axis=0), core])

def blursmear(core, band):         # clamp, blur hard, restore the core
    sm = smear(core, band); out = sm.copy()
    for c in range(3):
        out[..., c] = ndi.gaussian_filter(sm[..., c], band / 6.0)
    out[band:] = core
    return np.clip(out, 0, 255)

def stretch(core, band):           # what an aspect-ratio resize really does
    return np.array(Image.fromarray(core).resize(
        (core.shape[1], core.shape[0] + band), Image.BICUBIC), float)

def biharmonic(core, band):        # the one that is an algorithm
    canvas = np.vstack([np.zeros_like(core[:band]), core]).astype(float)
    mask = np.zeros(canvas.shape[:2], bool); mask[:band] = True
    for c in range(3):
        canvas[..., c] = inpaint_biharmonic(canvas[..., c], mask,
                                            split_into_regions=True)
    return np.clip(canvas, 0, 255)
Enter fullscreen mode Exit fullscreen mode

biharmonic is skimage.restoration.inpaint_biharmonic: it solves a fourth-order PDE with the rows below the seam as the boundary condition. It is the only method in the list that attempts to continue content rather than copy, smear, or stretch it, which makes it the right baseline for "what a real algorithm gets you".

Sixteen product photos, all real product photography, stratified by how much structure touches the frame edge — a photo whose borders are seamless paper tests nothing about outpainting. Working width 320 px, band cut from the top of a centre crop. That's 16 photos × 3 band heights × 5 methods.

What the numbers say

Medians over the 16 photos, band = 0.25 W (1:1 → 4:5):

fill RMSE seam curvature texture reflection edge recall
the real band 0 1.02 0.87 1.00 0.056 100%
reflected 53.5 0.82 2.52 1.46 1.000 68.9%
clamped 43.7 0.00 1.26 1.04 — 67.3%
stretched 53.4 1.03 1.02 1.36 0.105 64.1%
clamped + blurred 35.0 5.27 8.07 0.03 −0.043 0%
biharmonic 34.9 1.04 1.57 0.16 0.124 6.3%

Four things in that table matter more than the ordering.

The seam is the one metric that cannot tell. The reflected band scores 0.82 — better than the real band's 1.02. Clamped scores 0.00, a perfectly invisible join, because a clamped band is a constant. The two fills that score worst on fidelity have the calmest joins of anything in the table. If you have ever shipped a fill because the join looked clean, this is why that check let you down: continuity at the seam is the cheapest property to satisfy, and every method that satisfies it does so by making the band boring.

The curvature is what actually shows. Reflection is continuous but its derivative isn't: 2.52 against the real band's 0.87. That's the crease you see when a mirrored product meets its own reflection — the tone matches and the slope doesn't. It gets worse as the band grows: 2.52 → 2.93 → 3.91 at 0.25, 0.50 and 0.78 W. So the tell is a second difference, and a first-difference check will never find it.

Fidelity and texture are anti-correlated, by construction. Everything that keeps the band's detail scores 43–54 RMSE (texture 1.0–1.5); everything that scores 35 erased the detail (texture 0.03–0.16). Reflection actually scores above 1.0 on texture, at 1.46, because it duplicates the sharpest rows near the seam into the band and inflates the energy it is being credited for. That's not a good fill. It's a fill that would pass a "does it look detailed" check.

A PDE solve ties with smearing a blurred edge. 34.9 against 35.0. Biharmonic is not nothing — it gets the join right (1.04) and makes no attempt to fake structure — but on a median photo it lands in the same place as a five-line blur, because both of them produce a smooth ramp. The difference is that biharmonic's ramp is the correct ramp and the blur's isn't, and RMSE over 16 photos doesn't care.

That last point is why the per-photo spread is worth printing. Biharmonic's median is 34.9 and its maximum is 114.8, on a photo where it did worse than every geometric hack in the table — a failure mode of the solver on that image, which I did not chase down and which is a fair reason not to put a PDE fill in a batch pipeline without a per-image sanity check.

The deleted band, the real answer, and three fills of it, with the seam marked and the metrics underneath

How much of this is about the content

The error is not a constant of the method; it's a function of what was hidden. Spearman correlation between the number of edges in the deleted band and each method's RMSE, at the smallest band:

fill ρ p
biharmonic +0.74 0.001
clamped + blurred +0.70 0.003
clamped +0.69 0.003
stretched +0.62 0.011
reflected +0.58 0.019

Everything fails in proportion to how much was actually there — which is the least surprising result in the article, and also the useful one: the band that costs nothing to extend is the band with nothing in it, and that's exactly the band that didn't need extending.

At 0.78 W those correlations dissolve (ρ +0.13 to +0.71, most p > 0.2). Not because the methods improved — because by then everything is wrong by 50 levels or more, and the ceiling is flat. Past a point, "how wrong" stops being a question.

The cheapest way to fake it is the easiest to catch

Reflection scores exactly 1.000 on a reflected band and 0.069 on a real one. That gap is large enough to make a detector, so I ran it as one: 16 padded bands against 32 untouched samples (16 real continuations, 16 photos split in half and compared against their own reflection — the case where a symmetric product might plausibly trip it).

Every padded band scored 1.000. The untouched set had a median of +0.017 and a maximum of +0.181. Any threshold between 0.20 and 0.99 separates them perfectly, so the threshold is not a tuning decision — the groups don't touch.

That is too clean to leave alone, because a detector that only catches unmodified reflection proves very little. So: how much work does it take to hide?

variant median r caught RMSE texture
reflected 1.000 100% 53.5 1.46
reflected + blur σ0.6 0.981 100% 52.2 1.20
reflected + blur σ1.2 0.880 38% 50.7 0.86
reflected + blur σ2.0 0.736 6% 49.4 0.57
reflected + noise σ5 0.854 19% 53.7 1.48
half reflected, half blurred 1.000 100% 38.0 0.73
reflected band flipped 0.025 0% 55.3 1.46

Blurring is the disguise that works, and the table shows why it isn't worth the effort. At σ2.0 the detector loses 94% of its catches, and the fill improves from 53.5 to 49.4 — because a soft wrong band is closer to the truth than a sharp wrong band, which is the only reason any of this improves. But 49.4 is still 14 RMSE behind clamped-and-blurred at 35.0, so the disguise doesn't converge on a good fill; it converges on a worse version of the naive one. And the texture column says what you'd see: 1.46 down to 0.57, well below the 1.0 of the band it replaced. Caught or not, a heavily blurred reflection has nothing in it.

The half-and-half mix is the interesting row. It is the best fill in this table at 38.0, and the detector refuses to lose it — because averaging a reflection with a near-constant band leaves the reflection's vertical differences dominant, so r stays at 1.000. Improving the image and hiding it pull opposite ways, which is the shape of this whole problem: the moment a fill starts producing something that resembles the missing content, it starts resembling it in a way that can be checked.

Flipping the band defeats the detector outright (0.025) at a cost of 1.8 RMSE.

Reflection score for padded bands and untouched photos, with the cost of each disguise

What I'd do with this

  • Shoot the frame, don't extend it. Every number above is a cost you don't pay if the band was captured. If a shot has to serve 1:1, 4:5 and 9:16, frame for the tallest and crop down; cropping is a resize and the resize problem has a known answer, with a master to compare against.
  • Judge the join with a second difference, not a first. The first difference told me reflection was fine. The curvature said 2.52 against 0.87.
  • Never reflect. It is the only fill in the table whose join is invisible, and it is the most detectable and among the least accurate. It also duplicates any object that crosses the seam, which is the failure a viewer notices first and the metrics above barely register.
  • Keep the band under a quarter of the width. Reflected padding's curvature triples from 0.25 W to 0.78 W, and blur's seam goes from 5.27 to 14.67. The failure is not linear in the band.
  • If you check other people's assets, check this first. The detector is 15 lines and it is the only check in the article with a clean separation. Reflected padding is not a subtle artefact; it is a signature.
  • Don't trust an average. Biharmonic's median is 34.9 and its worst case is 114.8.

The detector, in full:

def mirror_score(img, band, min_h=8):
    """Best r between the added band and the flipped core, over band heights.
    Compared on the vertical difference so a lighting gradient can't fake it."""
    g = img[..., 0] * .299 + img[..., 1] * .587 + img[..., 2] * .114
    added = np.diff(g[:band], axis=0)
    best = -1.0
    for h in range(min_h, band + 1):
        src = np.diff(g[band + 1:band + 1 + h][::-1], axis=0)
        a, b = added[-(h - 1):].ravel(), src.ravel()
        if a.std() > 1e-3 and b.std() > 1e-3:
            best = max(best, float(np.corrcoef(a, b)[0, 1]))
    return best
Enter fullscreen mode Exit fullscreen mode

Point it at any exported listing image whose aspect ratio doesn't match the camera's, and a score above 0.9 means the sides were reflected.

Fidelity against texture recovered, and how the gap grows with the band

What this doesn't cover

These are five fills I wrote, not the state of the art. A diffusion or patch-based model would score differently, and probably much better on RMSE — that's the point of having a protocol. What the protocol says is that the naive answers are wrong in measurable, separable ways, not that no good answer exists. If you run the same harness against a model, the numbers are directly comparable; that's why the fills and the six metrics fit in 250 lines of numpy.

The band is always the top, always cut from a centre crop. Horizontal extension, which is what a banner needs, may behave differently — reflections work the same way, but the structure of most product photos is bottom-heavy (shadow, surface) and top-light (backdrop), so the same method could be better or worse there. I have not run it and won't guess.

320 px working width. All the seams here are 1–3 px phenomena before the metrics aggregate them; at 2000 px the same fill is a 6 px crease and the curvature ratio would change. The ranking would probably hold; the absolute numbers wouldn't.

Fidelity is not preference. Reproducing the deleted band is one thing a viewer wants and not the only one — a fill that invents a different plausible backdrop could score 50 RMSE and look better than a band that reproduces the original wall at 10. This protocol cannot see that, and no protocol I know of can without a human study.

Biharmonic's blow-up is unexplained. One photo in sixteen, 114.8 RMSE against a median of 34.9. I checked that it isn't a channel overflow and stopped there. If someone knows the failure mode, I'd like to.

I work on PicViva, where smart expand exists to turn square listing photos into the taller frames marketplaces and ad placements want. The reason this got measured instead of eyeballed is that the feature has to decide, per image, how much of the frame it is allowed to invent — and the numbers above are the argument for saying "not much" more often than the slider implies. A quarter of a width is a fill. Three quarters is a different picture.

If you've caught one of these in the wild — a reflected band on a competitor's banner, or your own old export — I'd like to know what the score was.

Top comments (0)