A lithophane is a thin 3D print that shows a photo only when light shines through it. Thick parts block light and look dark; thin parts glow. That makes the core of any photo-to-lithophane tool a single function: take a pixel's brightness and return a wall thickness in millimetres.
It sounds trivial, and the first version is. The interesting part is why the naive version prints muddy pictures, and what a few lines of extra math fix. This post walks through it with plain JavaScript you can drop into a canvas experiment.
Step 1: get brightness from a pixel
Start with an image drawn to a <canvas> and read its pixels:
const ctx = canvas.getContext("2d"); ctx.drawImage(img, 0, 0, w, h); const { data } = ctx.getImageData(0, 0, w, h); // RGBA, 0-255 function luminance(i) { const r = data[i] / 255, g = data[i + 1] / 255, b = data[i + 2] / 255; // Rec. 709 weights: green dominates perceived brightness return 0.2126 * r + 0.7152 * g + 0.0722 * b; } Averaging R, G and B equally is a common shortcut, but it makes blue skies too bright and green foliage too dark. The weighted version matches what people actually see in the finished print much better.
Step 2: the naive linear map
Pick a thinnest wall (for the brightest pixels) and a thickest wall (for the darkest). A widely used starting range for white PLA and a 0.4 mm nozzle is 0.8 mm and 3.0 mm, which is also the default on the best lithophane thickness guide I keep open while testing.
const T_MIN = 0.8; // mm, brightest const T_MAX = 3.0; // mm, darkest function thicknessLinear(lum) { return T_MAX - lum * (T_MAX - T_MIN); } Print that, and the result is usually disappointing: the shadows block up into one dark mass and most of the tonal detail sits in a narrow band of bright areas.
Why linear is wrong: light falls off exponentially
Light passing through a diffusing plastic does not drop by a fixed amount per millimetre. It drops by a fixed fraction. For white PLA, a rough rule of thumb is that each extra millimetre lets through about a quarter as much light. That is the Beer-Lambert idea in practice:
transmitted = exp(-k * thickness) If each millimetre keeps roughly 25% of the light, then k is about ln(4), or 1.39 per mm. With that, a 0.8 mm wall passes about 33% of the light and a 3.0 mm wall about 1.6%. That ratio, roughly 20 to 30 to 1 depending on your filament and LED, is the contrast range your print can show.
A linear thickness map spends equal millimetres on equal steps of brightness, but the light does not respond equally to those millimetres. The first millimetre of extra plastic dims things dramatically; the last one barely changes anything.
Step 3: map brightness to transmitted light, then solve for thickness
The fix is to decide how much light each pixel should let through, then invert the exponential to find the thickness that delivers it.
const K = Math.log(4); // ~1.39 per mm, tune for your filament const tMax = Math.exp(-K * T_MIN); // brightest transmission const tMin = Math.exp(-K * T_MAX); // darkest transmission function thicknessPhysical(lum, gamma = 2.2) { // undo sRGB-ish encoding so we work in linear light const linear = Math.pow(lum, gamma); // target transmission between the darkest and brightest walls const target = tMin + linear * (tMax - tMin); // invert exp(-K * t) = target return -Math.log(target) / K; } Two things happen here. The gamma step converts the photo's stored values back into linear light, because image files store brightness compressed for screens. Then the inverse log spreads the thickness changes so that equal steps in the photo become roughly equal steps in the glow.
Step 4: clamp and smooth
Real printers have limits. Walls thinner than about 0.6 mm get fragile and can leave pinholes that shine as bright dots, and anything thicker than about 3.5 mm mostly adds print time because the light is already gone. Clamp to your range and add a tiny blur so single noisy pixels do not become spikes in the mesh:
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v)); function heightMap(w, h) { const out = new Float32Array(w * h); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const i = (y * w + x) * 4; out[y * w + x] = clamp(thicknessPhysical(luminance(i)), T_MIN, T_MAX); } } return boxBlur(out, w, h, 1); // 3x3 average is plenty } From here the height map becomes a mesh: one vertex per pixel on the front face, a flat back, and side walls to close the solid so slicers accept it.
Step 5: tune for your light, not your screen
The constant K and the two walls depend on your filament and your light source far more than on your code:
- A bright light box or a sunny window washes out shadows, so raise the thickest wall toward 3.2 to 3.5 mm.
- A weak tea-light LED needs the opposite: drop the thickest wall to around 2.5 mm.
- Resin is more translucent than PLA, so the whole range shifts thinner.
Print a small step wedge (say 0.6, 0.8, 1.2, 1.6, 2.0, 2.5, 3.0 mm) in your filament, hold it to the light you will actually use, and fit K to what you see. Ten minutes of printing saves hours of failed portraits.
A shortcut if you just want the print
If you would rather skip the mesh code, Lithophanes does this in the browser with a backlit 3D preview of the actual mesh and a free flat-panel STL download, using the same 0.8 / 3.0 mm defaults. But building the brightness-to-thickness map yourself is a great small project: it touches color science, a little physics and mesh generation, and you get something you can hold up to a lamp at the end.
