← All articles 8 min read

Replace Color in Image: 5 Precise Methods That Work

You have a product photo of a red handbag, and the client wants navy blue. A brand refresh changed the primary green to teal, and forty assets need updating. The task is always the same: select one specific color and swap it for another, leaving everything else untouched.

This is different from a global hue shift, which moves every color at once. Replacing a color targets a precise range — "change only the reds" — while blues, skin tones, and shadows stay put. The key concept is tolerance (called "fuzziness" in Photoshop, "fuzz" in ImageMagick): how similar a pixel must be to your target before it gets replaced. Too low and you miss shadow variations. Too high and replacement bleeds into neighboring objects.

Five methods, ordered from most visual control to most automation.


Quick Reference: Method Comparison

Method Best For Tolerance Control Batch Support Cost Platform
Photoshop v26.3 Replace Color Precise visual edits Fuzziness slider (0-200) Via Actions $22.99/mo Win/Mac
Photoshop v26.3 Color Range + Hue/Sat Complex selections with masking Fuzziness + manual mask Via Actions $22.99/mo Win/Mac
GIMP v2.10.38 Free precise replacement Threshold slider (0-255) Script-Fu Free (GPL) Win/Mac/Linux
ImageMagick v7.1.1 CLI batch processing -fuzz percentage Yes (scripting) Free (Apache 2.0) All
Python Pillow 10.4 Full programmatic control Custom distance function Yes (scripting) Free (MIT) All

Photoshop v26.3: Replace Color Dialog

Photoshop's dedicated Replace Color tool combines selection and color shifting in a single dialog.

Steps

  1. Open your image in Photoshop v26.3.
  2. Go to Image > Adjustments > Replace Color.
  3. Click the eyedropper tool on the color you want to replace in the image preview. The white areas in the preview show what is selected.
  4. Adjust the Fuzziness slider. Start at 40 and increase until the full object is selected without spilling into surrounding areas. For a solid-colored product on a white background, 30-50 works well. For a textured object like fabric, try 60-100.
  5. Use the + eyedropper to add color variations (shadows, highlights) to the selection.
  6. Under Replacement, drag the Hue slider to your target color. Adjust Saturation and Lightness to match the brightness you want.
  7. Click OK.

Replace Color works best on objects with a distinct color — a red car on a gray road, a green logo on white. It struggles when the target color appears elsewhere (red shirt near red curtains) because there is no spatial control. For that, use Color Range below.

Limitation: This is a destructive edit. Duplicate the layer first (Ctrl+J / Cmd+J) so you can revert.


Photoshop v26.3: Color Range + Hue/Saturation

This method separates selection from replacement, giving you an editable mask.

Steps

  1. Go to Select > Color Range.
  2. Click the target color. Adjust Fuzziness until the preview captures the full object. Enable Localized Color Clusters if the same color appears elsewhere you do not want to change.
  3. Click OK to load the selection (marching ants appear).
  4. Go to Layer > New Adjustment Layer > Hue/Saturation. The active selection automatically becomes the layer mask.
  5. Check Colorize if you want an absolute color rather than a relative hue shift.
  6. Drag Hue to the replacement color. Adjust Saturation and Lightness.
  7. Switch to the Brush tool with black/white paint to refine the mask — black excludes areas, white includes them.

Why This Beats Replace Color

The layer mask is non-destructive and fully editable. For product mockups where you need multiple color variants, duplicate the adjustment layer and change the Hue value — no re-selection needed. Stack five Hue/Saturation layers with different hues and toggle visibility for each variant.


GIMP v2.10.38: Select by Color + Colorize

GIMP handles color replacement in two stages: select the color, then recolor.

Steps

  1. Open the image in GIMP v2.10.38.
  2. Select the By Color Select tool (Shift+O).
  3. In the tool options, set Threshold to control tolerance. A value of 15 is tight and exact; 50 captures broader variations. Start at 30.
  4. Click the color you want to replace. GIMP selects all pixels within the threshold of that color across the entire image.
  5. Hold Shift and click additional areas to add them to the selection, or increase the threshold and re-click.
  6. Go to Colors > Hue-Saturation. Select the matching primary color channel (R, Y, G, C, B, M) and drag the Hue slider. This shifts only the selected pixels.
  7. Alternatively, for an absolute color: go to Colors > Colorize, then set Hue, Saturation, and Lightness to the target values.

Spatial Limitation and Workaround

GIMP's Select by Color tool selects all matching pixels globally. If a red shirt and red lipstick both fall within the threshold, both get selected. The workaround: before step 2, use the Free Select (lasso) tool to draw a rough selection around the object you want to recolor. Then switch to By Color Select and hold Shift to intersect — GIMP will only select matching pixels within the lasso region.


ImageMagick v7.1.1: -fuzz -fill -opaque

ImageMagick replaces colors in a single command — the go-to for batch processing and CI/CD pipelines.

Basic Replacement

# ImageMagick v7.1.1 — replace red with blue
magick input.png -fuzz 15% -fill "#0000FF" -opaque "#FF0000" output.png

What each flag does:

Choosing the Right Fuzz Value

The fuzz factor is the most important parameter. Here is how different values behave on a typical product photo:

Fuzz Behavior Use Case
5% Very strict — only near-exact matches SVG graphics, flat illustrations
10-15% Moderate — catches minor compression artifacts Clean product photos on solid backgrounds
20-30% Broad — captures shadows and highlights Photos with lighting gradients
40%+ Aggressive — likely bleeds into other colors Rarely useful; prefer masking instead

Batch Processing

Process every PNG in a directory:

# ImageMagick v7.1.1 — batch replace red with blue
for file in products/*.png; do
  magick "$file" -fuzz 20% -fill "#0000FF" -opaque "#FF0000" "recolored/$(basename "$file")"
done

Replacing Multiple Colors

Chain multiple -fill -opaque pairs, each with its own fuzz value:

# ImageMagick v7.1.1 — replace red AND green
magick input.png \
  -fuzz 15% -fill "#0000FF" -opaque "#FF0000" \
  -fuzz 10% -fill "#FFFF00" -opaque "#00FF00" \
  output.png

Transparent pixels (PNG alpha channel) are never matched — no extra flags needed.


Python Pillow 10.4: Pixel-Level Replacement

When you need custom logic — region-limited replacement, gradual transitions, or integration into a larger pipeline — Python gives you full control.

# Python 3.12 + Pillow 10.4
from PIL import Image
import colorsys, math

def replace_color(image_path, target_rgb, replacement_rgb, tolerance=50, preserve_brightness=True):
    """Replace target_rgb with replacement_rgb within tolerance.
    If preserve_brightness=True, keeps original shading (recommended for photos)."""
    img = Image.open(image_path).convert("RGB")
    pixels = img.load()
    width, height = img.size
    repl_hsv = colorsys.rgb_to_hsv(*(c / 255.0 for c in replacement_rgb))

    for y in range(height):
        for x in range(width):
            r, g, b = pixels[x, y]
            dist = math.sqrt(sum((a - b_) ** 2 for a, b_ in zip((r, g, b), target_rgb)))
            if dist <= tolerance:
                if preserve_brightness:
                    _, _, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
                    nr, ng, nb = colorsys.hsv_to_rgb(repl_hsv[0], repl_hsv[1], v)
                    pixels[x, y] = (int(nr * 255), int(ng * 255), int(nb * 255))
                else:
                    pixels[x, y] = replacement_rgb
    return img

# Replace red with blue, tolerance 60, preserve shading
result = replace_color("input.png", (255, 0, 0), (0, 0, 255), tolerance=60)
result.save("output.png")

With preserve_brightness on, dark folds stay dark and highlights stay bright — matching how the object would actually look in the new color. Without it, every matched pixel becomes identical, erasing all shading. Wrap in a pathlib.Path.glob() loop for batch processing.


Choosing the Right Method

Need to convert your images to a different format after recoloring? Pixotter handles format conversion, compression, and resizing in one step — directly in your browser.


FAQ

How do I replace a specific color without affecting similar colors nearby?

Use a method with spatial control. In Photoshop v26.3, Select > Color Range with Localized Color Clusters restricts selection to a region around your click. In GIMP v2.10.38, draw a lasso selection first, then use Select by Color within that area. In Python, add coordinate bounds to the pixel loop.

What is the difference between replacing a color and shifting the hue?

A hue shift moves every color by the same amount on the color wheel. Replacing a color targets one specific color and swaps it, leaving everything else unchanged. Use hue shifting for global mood changes; use replacement for changing a single object's color.

Can I replace a color in a JPEG without quality loss?

Not entirely. JPEG is lossy — saving recompresses the image. Minimize degradation by saving at quality 95+, or convert to PNG before editing and export to JPEG only at the final step. JPEG compression artifacts also spread color variation, so you will need a higher tolerance than with PNG sources.

How do I replace white or black in an image?

Keep tolerance very low (fuzz 5-8% in ImageMagick, fuzziness 10-20 in Photoshop) because many objects contain white highlights and black shadows. Use spatial selection (Photoshop's Localized Color Clusters or GIMP's lasso-then-select approach) to limit the replacement area. See our background color guide for replacing white backgrounds specifically.

Can I replace a color with a transparent one?

Yes. In ImageMagick v7.1.1, use -transparent instead of -fill -opaque:

magick input.png -fuzz 15% -transparent "#FF0000" output.png

The output must support alpha (PNG, WebP, TIFF — not JPEG). In Python Pillow, convert to RGBA mode and set alpha to 0 for matched pixels.

What tolerance value should I start with?

Flat illustrations: low (fuzz 5-10%, fuzziness 20-30, Python distance 25). Product photos with even lighting: medium (fuzz 15%, fuzziness 40-60, distance 50). Photos with strong lighting variation: high (fuzz 25%, fuzziness 80-100, distance 70). Check edges after the first pass and adjust in small increments.

How do I get the exact hex code of the color I want to replace?

In Photoshop and GIMP, the eyedropper tool shows the exact color of any pixel you click. Browsers have a built-in picker in dev tools (Chrome: inspect element, click the color swatch in Styles). Our color picker tool extracts exact hex values from any uploaded image.

Can I replace colors in SVG files without rasterizing?

Yes. SVGs store colors as text attributes (fill="#FF0000"), so find-and-replace hex codes in any text editor or with sed -i 's/#FF0000/#0000FF/g' icon.svg. No tolerance needed — SVG colors are exact values.