DEV Community

Om Prakash
Om Prakash

Posted on

How to Remove Unwanted Objects from Photos with AI (Without the Photoshop Bill)

How to Remove Unwanted Objects from Photos with AI (Without the Photoshop Bill)

Every photo has something in it that shouldn't be there.

A power cable across an otherwise clean product shot. A watermark from a stock photo you licensed but can't reshoot. Tourists in a venue photo. A logo on a garment that creates licensing complications. Price tags, stickers, sensor dust.

Photoshop's content-aware fill solves this brilliantly — but it costs $55/month, requires manual work per image, and doesn't batch. Here's the API equivalent.

LaMa: The Model Behind Serious Object Removal

LaMa (Large Mask) was developed at Samsung AI for exactly this problem — removing objects from photos while reconstructing the background convincingly. Unlike older inpainting approaches that tile neighboring pixels, LaMa uses Fourier convolutions to understand and regenerate large missing regions coherently.

It's particularly strong at:

  • Regular textures (wood grain, fabric, brick, grass)
  • Objects against simple or repetitive backgrounds
  • Watermark removal
  • Product cleanup

Basic Usage

import requests, base64

def remove_object(image_url: str, mask_url: str, api_key: str) -> str:
    """
    image_url: URL of the original image
    mask_url:  URL of a black/white mask (white = area to remove)
    Returns: URL of cleaned image
    """
    response = requests.post(
        "https://api.pixelapi.dev/v1/edit",
        headers={"Authorization": f"Bearer {api_key}"},
        json={
            "operation": "remove-object",
            "image_url": image_url,
            "mask_url": mask_url
        }
    )
    return response.json()["output_url"]
Enter fullscreen mode Exit fullscreen mode

The mask is a simple black/white image the same dimensions as the original. White pixels = "remove this". Black pixels = "leave this alone".

Creating Masks Programmatically

For automated pipelines, generate masks with OpenCV:

import cv2, numpy as np

def create_simple_mask(image_path: str, x: int, y: int, w: int, h: int) -> str:
    """Create a rectangular mask over specified region."""
    img = cv2.imread(image_path)
    mask = np.zeros(img.shape[:2], dtype=np.uint8)  # Black canvas
    mask[y:y+h, x:x+w] = 255  # White rectangle = remove this
    mask_path = "/tmp/mask.png"
    cv2.imwrite(mask_path, mask)
    return mask_path

# Example: remove a 200x50px watermark from bottom-right corner
mask = create_simple_mask("product.jpg", x=800, y=950, w=200, h=50)
Enter fullscreen mode Exit fullscreen mode

Real Pipeline: Watermark Removal at Scale

import requests
import boto3
from concurrent.futures import ThreadPoolExecutor

def process_image(s3_key: str, api_key: str, mask_url: str) -> dict:
    # Generate presigned URL for the image
    image_url = generate_presigned_url(s3_key)

    # Remove object
    clean_url = remove_object(image_url, mask_url, api_key)

    # Download and re-upload to permanent storage
    clean_bytes = requests.get(clean_url).content
    clean_key = s3_key.replace("raw/", "processed/")
    upload_to_s3(clean_bytes, clean_key)

    return {"original": s3_key, "processed": clean_key}

# Batch process with thread pool
with ThreadPoolExecutor(max_workers=5) as executor:
    results = list(executor.map(
        lambda key: process_image(key, API_KEY, WATERMARK_MASK_URL),
        s3_image_keys
    ))
Enter fullscreen mode Exit fullscreen mode

When It Works Brilliantly vs When It Struggles

Works well:

  • Watermarks on textured backgrounds
  • Price tags, stickers, labels
  • Power cables and wires over complex backgrounds
  • Unwanted people in background of venue photos
  • Skin blemishes and temporary marks

More challenging:

  • Objects that occlude structural elements (a person standing in front of an architectural feature — the model must reconstruct what's behind them)
  • Very large masks (>30% of image area)
  • Objects at the intersection of multiple different materials

For difficult cases: use a slightly smaller mask than you think you need, and run multiple passes if needed.

Cost vs Alternatives

Cloud photo editing APIs with object removal:

  • Adobe Firefly API: enterprise pricing, waitlist
  • Cleanup.pictures: ~20 credits/image on their plan
  • Manual Fiverr edits: high per-image cost at any volume

PixelAPI: 20 credits/image. At Starter plan, that's 500 object removals from a single purchase.

Getting Started

pixelapi.dev — 100 free credits. That's 5 test removals to verify it handles your use case before committing.

API reference: api.pixelapi.dev/docs


LaMa inpainting runs on PixelAPI's dedicated RTX GPUs. Results available within 3-5 seconds for typical image sizes.

Top comments (0)