DEV Community

Louis
Louis

Posted on

Replace a product photo’s background without repainting the product

Replace a product photo’s background without repainting the product

By Louis, developer of RemixThatPhoto

Put a white ceramic mug on a pale desk and photograph it from the side. The interesting part of a background swap is the hole in the handle. A flat cutout can look fine on white while a little island of the old desk still floats inside the handle on blue. Here’s a way to make one reusable cutout, check that opening, and export two versions for free.

This tutorial was drafted with AI assistance.

Make a mask you can inspect

  1. Open your own mug photo in GIMP. Keep the original file. If the mug is blurry or the handle blends completely into the desk, take another shot with a sheet of contrasting paper behind it. This workflow works best for opaque objects with a visible outline.
  2. Choose Layer → Transparency → Add Alpha Channel on the photo layer if it is available. Use Free Select to trace the outside silhouette. Zoom in on the rim, click more frequently along curves, and press Enter to finish. Then switch the tool to Subtract from the current selection, trace the opening inside the handle and press Enter again. You should see two selection outlines.
  3. Right-click the photo layer, choose Add Layer Mask, and initialize it from Selection. Choose Select → None. The desk and the handle opening should now show the checkerboard. Save the layered file as mug.xcf so you can return to the mask.
  4. Create a solid blue layer below the masked photo. Change it to near-black, then white, and inspect the rim and the inside of the handle at 100%. If a crescent of desk appears, click the mask thumbnail and paint black over the crescent with a small brush. If the mug has a notch, paint white to bring that section back. Use a soft brush only where the original edge is soft. Leave the solid middle of the mug alone.
  5. Hide the solid layer before exporting mug-cutout.png. Reopen the PNG on its own. Look for checkerboard in a corner and inside the handle. If you see blue there, the background layer was visible during export. An opaque JPEG cannot carry this cutout.

The blue layer is a diagnostic. Against a white background, edge pixels that still contain light from the original desk may be almost invisible. Against blue, they form a pale outline. Shrinking the entire mask can cost you the thin lip of the mug. Adjust the few pixels where the outline actually shows.

Render two backgrounds from the same cutout

In GIMP you can change the lower layer’s color and export each version. For a repeatable export, the following Python script uses the PNG’s alpha channel as the mask. Install Pillow in a virtual environment with python -m pip install Pillow, put the script beside the PNG, and run python render_mug.py.

from pathlib import Path
from PIL import Image

cutout = Image.open("mug-cutout.png").convert("RGBA")
alpha = cutout.getchannel("A")
if alpha.getextrema() != (0, 255):
    raise ValueError("Expected a transparent background and an opaque mug center.")

for name, rgb in {"sand": (236, 224, 204), "navy": (24, 40, 66)}.items():
    backdrop = Image.new("RGBA", cutout.size, (*rgb, 255))
    result = Image.alpha_composite(backdrop, cutout).convert("RGB")
    result.save(Path(f"mug-{name}.png"))
Enter fullscreen mode Exit fullscreen mode

Open both exports at the same zoom. The center of the mug should keep the same color in both. The handle opening should switch from sand to navy. A partially transparent edge should change a little because it blends with the new background. If the center changes, inspect that area of the mask for gray brush strokes. If the mug seems to float, the original contact shadow was outside the cutout. A catalog cutout can stand alone, but a believable tabletop scene needs a shadow consistent with the new surface and light.

I develop RemixThatPhoto, which also has a New Background mode. The same handle-opening check works on a generated edit. For a product label whose lettering must stay exactly as photographed, the masked original gives you a reference you can check pixel by pixel.

Top comments (0)