Most small sellers shoot product photos on a phone, indoors, in winter light. The result is usually not blurry, just dark: the product sits in the bottom half of the histogram and the white backdrop comes out grey. I needed a one-click "make this brighter" fix for a browser tool, with three constraints:
- It must never darken anything. Users press it because the photo is too dark, so the shadows they already have shouldn't get crushed.
- It must not shift colours. A cream candle should stay cream, not turn pink.
- It has to run on a 2000 px image in the browser without a library.
Here's the whole thing, about 30 lines, taken from the tool's source.
1. A luminance histogram, sampled
Reading every pixel of a 2000 × 2000 image is 4 million iterations. For statistics we don't need all of them, so the loop takes about 400k evenly spaced samples:
const ctx = canvas.getContext('2d', { willReadFrequently: true }) const img = ctx.getImageData(0, 0, canvas.width, canvas.height) const d = img.data const hist = new Uint32Array(256) const step = Math.max(1, Math.floor((d.length / 4) / 400_000)) let n = 0 for (let i = 0; i < d.length; i += 4 * step) { hist[Math.round(0.2126 * d[i] + 0.7152 * d[i + 1] + 0.0722 * d[i + 2])]++ n++ } The weights are the Rec. 709 luma coefficients applied directly to sRGB values. That's not physically exact (you'd linearize first), but for "is this photo dark" it's close enough and cheap.
2. Pick a black point and a white point
Classic auto-levels stretches the darkest pixel to 0 and the brightest to 255. Using the true min and max is fragile: one specular highlight or one black pixel and nothing happens. Percentiles are more robust:
const pctl = (q) => { let acc = 0 for (let v = 0; v < 256; v++) if ((acc += hist[v]) >= q * n) return v return 255 } const lo = Math.min(pctl(0.005), 40) // black point, capped const hi = Math.max(pctl(0.995), lo + 60) // white point, at least 60 above lo Two guards here:
-
lois capped at 40. A photo whose darkest 0.5% is already at 70 is probably a light product on a light backdrop; stretching from 70 would make it harsh. -
histays at least 60 abovelo. On a nearly flat image (a white mug on a white sheet),hi - locan be tiny, and dividing by it would blow up noise.
3. Solve a gamma that moves the mean towards mid-grey
After the linear stretch, many dark photos are still dark, because most pixels sit low in the range. A gamma curve lifts the midtones without touching black or white. I want the average brightness to end up around 125 out of 255:
let mean = 0 for (let v = 0; v < 256; v++) { mean += hist[v] * Math.min(1, Math.max(0, (v - lo) / (hi - lo))) } mean = Math.max(0.02, mean / n) // stretched mean, 0..1 const g = mean >= 125 / 255 ? 1 : Math.max(0.5, Math.log(125 / 255) / Math.log(mean)) mean ** g = 125/255 gives g = log(125/255) / log(mean). Strictly, the mean of t ** g is not mean(t) ** g (Jensen's inequality), so the result lands near the target rather than on it. For a one-click fix that's fine, and it avoids an iterative solve.
The clamp matters more than the formula: g never goes below 0.5 (no extreme lift on a nearly black photo) and never above 1 (an already bright photo is left alone).
4. One lookup table, applied to all three channels, never darker
const lut = new Uint8ClampedArray(256) for (let v = 0; v < 256; v++) { const t = Math.min(1, Math.max(0, (v - lo) / (hi - lo))) lut[v] = Math.max(v, Math.round(255 * Math.pow(t, g))) // only ever brighter } for (let i = 0; i < d.length; i += 4) { d[i] = lut[d[i]] d[i + 1] = lut[d[i + 1]] d[i + 2] = lut[d[i + 2]] } ctx.putImageData(img, 0, 0) -
Math.max(v, …)is the "never darken" rule. Values belowlowould otherwise map to 0, so deep shadows would get crushed. With the max, every output value is greater than or equal to its input. - The same curve for R, G and B. Per-channel auto-levels (stretching each channel to its own percentiles) also corrects white balance, which sounds nice but is exactly what shifts a cream product towards white or pink. One shared curve changes brightness and leaves the colour relationships mostly intact. Saturation can still drift a little in the midtones, since the curve isn't linear; for this use it's an acceptable trade.
- A 256-entry lookup table means the per-pixel work is three array reads, so the full-resolution pass is quick.
5. Bonus: export as a JPEG under 1 MB
Etsy's help pages say files over 1 MB may not finish uploading on a slow connection, so the same tool can save the result under that size. canvas.toBlob takes a JPEG quality, and size goes down roughly as quality goes down, so the export walks a short ladder and stops at the first one that fits:
async function jpegUnder(canvas, maxBytes) { const enc = (q) => new Promise((res, rej) => canvas.toBlob((b) => (b ? res(b) : rej(new Error('export failed'))), 'image/jpeg', q)) let last = null for (const q of [0.92, 0.88, 0.84, 0.8, 0.75, 0.7, 0.62, 0.55, 0.5]) { last = await enc(q) if (last.size <= maxBytes) return last } return last // still too big at 0.5: the caller suggests resizing instead } Why a fixed ladder and not a binary search? It starts at high quality and stops as soon as the file fits, so a photo that already fits costs a single encode, and the result is never compressed more than it needs to be (within one step). It also never picks a quality below 0.5. Below that, JPEG artefacts on a plain backdrop become visible, and resizing is the better fix, so the UI says that instead.
One policy caveat
Etsy now requires at least one unedited photo of the actual item per listing. Since November 6, Etsy allows cropping, resizing and watermarking on that photo (not with AI tools), but not brightness or colour changes. So a fix like this belongs on your other listing photos, and the required unedited one should be shot bright enough in the first place.
Try it
I built this into a free Etsy listing photo checker for my side project ImageWhim. It also checks size, blur and collage-style first photos. It runs entirely in the browser, and your photo is never uploaded: https://imagewhim.com/tools/etsy-listing-photo-checker
Disclosure: I'm the developer of ImageWhim. This post was written with AI assistance; the code is taken from the tool's actual source.


