DEV Community

Jay
Jay

Posted on Fully Autonomous

Your product photo background isn't 255 — and one brightness slider won't fix it

You export a product photo on white and open it in a marketplace uploader. The preview looks fine. Then the background check fails. On an 8-bit image, a neutral pixel at 224 can look white next to a dark product, especially when there’s no pure-white reference nearby.

That’s what makes a brightness slider tempting. If the white sweep is too dark, turn everything up. The trouble appears when you move the eyedropper. A sweep can be made of one white material and still have an 84-level difference between its darkest and brightest sampled points.

What one image showed

The test image was a generated cover at 1536 × 864. The background measurement used the outer 40 px border band. In the original, the band averaged 224.7, with a minimum of 160 and a maximum of 244. Exactly zero pixels in that band reached 250.

Metric Original Global ×1.126 Flat-field correction
Mean border-band brightness 224.7 253.0 251.4
Border-band pixels at or above 250 0.0% 68.2% 82.6%
Clipped pixels across the full image 0% 42.6% 0%
Clipped pixels in the central region (30–70%) 0% 11.3% 0%

The table preserves the reported measurements from this single image. The global run is labeled ×1.126 in the experiment notes. Dividing 255 by the reported mean of 224.7 gives about 1.135, so that mean alone cannot explain the recorded setting.

The global run had the higher border mean. The field-corrected run had more of the border above the threshold. Those two measurements tell you different things. Saturated areas can pull up an average while a dark stretch remains visible. The center-region reading gives you a separate view of how much clipping occurred around the subject, where small brightness differences can carry the shape of a rounded edge.

This is one generated image and one 40 px measurement band. It’s a worked example of the trade-off, not an industry benchmark. The ≥250 count is a diagnostic from this test. A rule that requires pure white needs its own check for RGB (255, 255, 255) in the relevant background area.

Estimate the light across the sweep

Think of the brightness of the white sweep as a field that varies across the frame. One multiplication scales the dark end and the bright end together. For a neutral pixel at 160 to reach 255, the gain would need to be about 1.59. Applying that gain to 244 takes it well beyond what an 8-bit channel can store. A smooth field gives the darker area a larger correction and the brighter area a smaller one.

Start with the median brightness of an outer ring. A small dark object touching one edge can pull the mean away from what most of the ring looks like. The median gives you a reference for a first pass. Keep pixels whose brightness exceeds that reference minus 14. This is a brightness filter.

Fit the six terms [1, u, v, u², v², uv] by least squares. Normalize the coordinates to [−1, 1] before forming the normal equations. Squared raw pixel coordinates are much larger than the constant term, and forming the normal equations further amplifies conditioning problems. The six coefficients describe a smooth surface with enough flexibility for a gradual falloff across a simple sweep.

Reject outliers before using the field

A contact shadow can be light enough to pass the initial filter. When those samples enter the least-squares fit, they pull the estimated field down near the object. The correction then divides by that smaller value and lifts the region that the shadow was supposed to darken. You end up flattening the shadow instead of the lighting.

After each fit, compare the observed brightness to the field at that position. Drop points with an absolute residual over 7 and fit again. The sample below allows four rejection rounds and refits the remaining points for output. This works when the clean sweep supplies enough samples across the frame. A broad shadow or a bright product can still dominate the fit.

A JavaScript implementation

The example uses sharp’s raw pixel output. It fits the field to weighted RGB values in decoded sRGB bytes. That makes it an image-space approximation for this kind of edit. Clamp the field to [120, 255] before dividing, then round and cap the output bytes. Exporting to PNG keeps another compression step out of subsequent pixel measurements.

import sharp from 'sharp'

export async function correctWhiteSweep(input, output) {
  const { data, info } = await sharp(input)
    .toColourspace('srgb')
    .flatten({ background: '#ffffff' })
    .raw()
    .toBuffer({ resolveWithObject: true })

  const { width, height, channels } = info
  if (width < 2 || height < 2 || channels !== 3) {
    throw new Error('Expected an RGB image at least 2 × 2')
  }

  const luma = (x, y) => {
    const i = (y * width + x) * 3
    return 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]
  }
  const basis = (x, y) => {
    const u = 2 * x / (width - 1) - 1
    const v = 2 * y / (height - 1) - 1
    return [1, u, v, u * u, v * v, u * v]
  }
  const ring = []
  for (let x = 0; x < width; x++) {
    ring.push(luma(x, 0), luma(x, height - 1))
  }
  for (let y = 1; y < height - 1; y++) {
    ring.push(luma(0, y), luma(width - 1, y))
  }
  ring.sort((a, b) => a - b)
  const mid = Math.floor(ring.length / 2)
  const reference = (ring[mid] + ring[mid - 1]) / 2

  let samples = []
  for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
      const value = luma(x, y)
      if (value > reference - 14) samples.push([x, y, value])
    }
  }

  const predict = (coefficients, x, y) =>
    basis(x, y).reduce((sum, term, i) => sum + term * coefficients[i], 0)

  function fit(points) {
    if (points.length < 6) throw new Error('Too few background samples')
    const matrix = Array.from({ length: 6 }, () => Array(7).fill(0))
    for (const [x, y, value] of points) {
      const terms = basis(x, y)
      for (let i = 0; i < 6; i++) {
        for (let j = 0; j < 6; j++) matrix[i][j] += terms[i] * terms[j]
        matrix[i][6] += terms[i] * value
      }
    }

    for (let col = 0; col < 6; col++) {
      let pivot = col
      for (let row = col + 1; row < 6; row++) {
        if (Math.abs(matrix[row][col]) > Math.abs(matrix[pivot][col])) {
          pivot = row
        }
      }
      const divisor = matrix[pivot][col]
      if (Math.abs(divisor) < 1e-12) throw new Error('Singular field fit')
      const previous = matrix[col]
      matrix[col] = matrix[pivot]
      matrix[pivot] = previous
      for (let j = col; j <= 6; j++) matrix[col][j] /= divisor
      for (let row = 0; row < 6; row++) {
        if (row === col) continue
        const factor = matrix[row][col]
        for (let j = col; j <= 6; j++) {
          matrix[row][j] -= factor * matrix[col][j]
        }
      }
    }
    return matrix.map(row => row[6])
  }

  let field = fit(samples)
  for (let round = 0; round < 4; round++) {
    const kept = samples.filter(([x, y, value]) =>
      Math.abs(value - predict(field, x, y)) <= 7
    )
    if (kept.length === samples.length) break
    samples = kept
    field = fit(samples)
  }

  const corrected = Buffer.alloc(data.length)
  for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
      const estimate = Math.max(120, Math.min(255, predict(field, x, y)))
      const gain = 255 / estimate
      const i = (y * width + x) * 3
      for (let c = 0; c < 3; c++) {
        corrected[i + c] = Math.min(255, Math.round(data[i + c] * gain))
      }
    }
  }

  await sharp(corrected, { raw: { width, height, channels: 3 } })
    .png()
    .toFile(output)
}
Enter fullscreen mode Exit fullscreen mode

The code caps output values, so it can still clip highlights. The 0% in the table is the reported result for the supplied example. If you instrument a new run, count pixels with at least one channel above 255 before capping, and record that definition beside the result. Counting output pixels equal to 255 would also pick up pixels that landed on 255 legitimately. The original notes don’t specify their clipping counter, and the table isn’t a benchmark of this standalone code.

When to skip the fit

Use this on a plain, single-color backdrop with uneven lighting. A lifestyle shot with a tabletop and wall has real edges and color changes. A single field wipes those variations together and can give the scene a plasticky finish. A white mug against a white sweep can also end up in the candidate set. The estimator sees two bright surfaces and needs a segmentation mask to tell them apart. If the contact shadow matters to the final image, skip the unmasked correction. Rejecting shadow samples stabilizes the fit, while the output gain still changes the pixels under the product.

If a corner of your exported image still fails the white check, inspect the subject highlights before raising the gain again, and try PicViva’s background removal tool if you need to isolate the product first.

Top comments (0)