AI & Creative Tools

Mask Anything in a Photo Without Drawing a Single Pixel

Segment Anything gives you a pixel-perfect mask from a single click or box — the missing step between 'generate an image' and 'edit exactly one part of it.'

Every inpainting or outpainting workflow asks for the same thing before it does anything useful: a mask. A black-and-white image marking exactly which pixels are fair game to change. Most tutorials assume you already have one, or tell you to draw it by hand in an image editor. You don’t have to.

DigitalSreeni’s walkthrough of running Segment Anything on your own images in Python.

The idea

Segment Anything (SAM) is a model trained to do one thing extremely well: given an image and a hint about what you’re pointing at — a click, a few clicks, or a rough bounding box — it returns a pixel-accurate mask of that object. No hand-drawing, no manual tracing around hair or foliage or glass.

The hint is the whole interface. Click once on a mug and SAM returns a mask of the mug, not the table it’s sitting on. Draw a loose box around a person and it tightens that box into the actual silhouette.

The minimum working version

pip install segment-anything torch torchvision opencv-python
# download a checkpoint, e.g. the base model:
# https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
from segment_anything import sam_model_registry, SamPredictor
import cv2

sam = sam_model_registry["vit_b"](checkpoint="sam_vit_b_01ec64.pth")
predictor = SamPredictor(sam)

image = cv2.imread("photo.jpg")
predictor.set_image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

# a single point click, in (x, y) pixel coordinates
masks, scores, _ = predictor.predict(
    point_coords=[[480, 320]],
    point_labels=[1],      # 1 = foreground, 0 = background
    multimask_output=True,
)

best_mask = masks[scores.argmax()]
cv2.imwrite("mask.png", (best_mask * 255).astype("uint8"))

That’s a working mask in roughly ten lines, running on CPU in a few seconds or on a consumer GPU in under one.

The one real gotcha: ambiguity

A single click is often genuinely ambiguous. Click on a shirt and SAM doesn’t know if you mean the shirt, the person wearing it, or just the sleeve. That’s why multimask_output=True returns three candidate masks at different granularities instead of one — the whole-object mask, a part-level mask, and a sub-part mask — each with a confidence score. Picking the highest-scoring one, as in the snippet above, works most of the time; for anything you’re about to publish, look at all three before committing, especially around hair, transparent objects, or anything with a complex silhouette.

A box prompt resolves ambiguity faster than a point when the object is well-defined: swap point_coords/point_labels for box=[x1, y1, x2, y2] and SAM will mask whatever best fills that box, which is usually exactly the object you boxed.

Feeding the mask forward

The mask PNG from this script is the direct input to an inpainting pipeline — white pixels mark what gets regenerated, black pixels mark what stays untouched. Pair this with the inpainting workflow in Inpainting and Outpainting: Getting Started to go from “click on the object” to “replace it with something else entirely” without ever opening a layers panel.