Hey everyone, it’s Jake from your go-to Pillow supplier—you know, the folks who hook you up with all the top-tier Python imaging tools you actually need, no hassle. Today we’re talking about something that’s been popping up in our support inbox nonstop for the past month: how to replace a color in an image using Pillow. Look, I get it—whether you’re tweaking product photos for your online store, fixing a weird color cast on a team meme, or prepping visuals for a client pitch, sometimes that one off-color is holding the whole thing back. And if you’re using Pillow, you’ve already got the perfect tool for the job—no need to download sketchy random apps or pay for overpriced editing software. Let’s break this down like we’re chatting over a coffee, no jargon, just real steps that work. Pillow

First off, a quick heads up: if you don’t already have Pillow installed, that’s the first tiny step. Most of you probably know this, but it’s easy to mix up package names (I’ve been there—once I tried installing “pillow” with a typo and wasted 10 minutes). So open your terminal or command prompt, type pip install pillow, and you’re golden. If you’re on a Mac or Linux, add --user at the end if you get a permission error, and boom—you’re ready to roll.
Now, why are we hyped about Pillow for this? It’s lightweight, fast, and since it’s a Python library, you can even automate this process if you’ve got 100 product shots to touch up (no more clicking through each one manually). And as your Pillow supplier, we stock the latest stable versions so you don’t run into weird bugs—trust me, there’s nothing worse than half-finished color edits because you’re on an old, glitchy release.
Let’s get to the actual code, because that’s what you’re here for. First, you need to load your image. Let’s say we’ve got a sample image named dog.jpeg—it’s a cute golden retriever, but that bright blue collar he’s wearing clashes with his yellow fur (bad doggy fashion choice, I know). We want to replace that exact blue with a nice matching orange. Here’s how to start:
from PIL import Image
from PIL.Image import Color
Load the image
img = Image.open("dog.jpeg")
Convert to RGB mode to make color picking easier (no weird transparency or grayscale mix-ups)
img = img.convert("RGB")
Wait, hold up—why convert to RGB? Because some images come in modes like RGBA (with a transparent background) or P (palette mode with limited colors), and trying to pick a color in those can give you wonky results. Converting to RGB locks in each pixel as three values: red, green, blue, all 0-255. That way, we know exactly what color we’re targeting.
Next step: pick the color you want to replace. This is the most important part—if you don’t get the exact RGB values right, your edit will look off. How do you do that? Open the image in any photo viewer (even the default one on your phone works) and use the color picker tool. For our blue collar, let’s say the RGB values are (25, 100, 220). Got it. Now, the color we want to replace it with—let’s say that’s (255, 150, 50), that warm orange.
Now, here’s the trick: loop through every single pixel in the image and check if it matches the target color. If it does, swap it for the new color. That sounds tedious, but Pillow makes it super straightforward. Let’s write that code:
Target color we want to replace (that blue collar)
target_color = (25, 100, 220)
New color to swap in
new_color = (255, 150, 50)
Get the pixel data as a list so we can modify it
pixels = img.load()
Loop through every x and y coordinate in the image
for x in range(img.width):
for y in range(img.height):
# Get the color of the current pixel
current_color = pixels[x, y]
# If it’s exactly the target color, replace it
if current_color == target_color:
pixels[x, y] = new_color
Wait a second—what if the color isn’t perfect? Like, maybe the blue collar has a tiny shade variation, not exactly (25,100,220). If you use exact matching, you’ll miss those areas, and the edit will look patchy. That’s a common mistake, so let’s fix that. Instead of checking if current_color equals target_color exactly, we can set a tolerance range. Like, if the color is within 20 points of the target on each RGB channel, we’ll replace it. That way, subtle shade differences don’t break the edit. Here’s how to adjust that part:
Tolerance value—play with this! 10-30 works for most cases
tolerance = 20
if (abs(current_color[0] – target_color[0]) < tolerance and
abs(current_color[1] – target_color[1]) < tolerance and
abs(current_color[2] – target_color[2]) < tolerance):
pixels[x, y] = new_color
Oh, that’s way better! I learned this trick when I was editing product photos for a local bakery a while back—their logo had a few tiny red variations, and exact matching left white gaps. Cranking the tolerance to 15 fixed it perfectly. Pro tip: if you’re not sure what tolerance to use, start with 10, run the code, check the result, then bump it up by 5 if you missed spots. Don’t go over 30, though—you might accidentally change parts of the image that weren’t supposed to be touched, like the blue sky in the background of a landscape shot.
Now, what about images with transparent backgrounds? Like PNGs with a see-through area. The code above works, but let’s adjust it to handle RGBA mode so you don’t mess up the transparency. Let’s rewrite the load part to keep alpha:
img = Image.open("dog.png")
Keep RGBA mode instead of converting to RGB, so transparency stays intact
img = img.convert("RGBA")
Now, target color will have an alpha value too—let’s say our blue collar has alpha 255 (fully opaque)
target_color = (25, 100, 220, 255)
new_color = (255, 150, 50, 255)
tolerance = 20
And the pixel check stays almost the same—just add the alpha channel to the tolerance check if you want to match semi-transparent colors. For example, if your target color has alpha 180 (semi-see through), add abs(current_color[3] - target_color[3]) < tolerance to the condition. Easy peasy.
Wait, what if you have a big image? Like a 4K photo of a building, and you only want to replace the color of one window. Looping through every single pixel is fine for small images, but it can be slow for huge ones (I’ve waited 5 minutes for a 10MP image to process once—total buzzkill). That’s where a Pillow trick with ImageChops comes in, which is way faster because it’s built on optimized C code, not Python loops. Let’s go over that method too, since a lot of folks ask about speed.
First, import ImageChops from Pillow:
from PIL import Image, ImageChops
Load your image, same as before
img = Image.open("city_building.jpg").convert("RGB")
target_color = (180, 200, 220) # that light gray window color
new_color = (50, 100, 150) # deep blue for the windows
Make a mask: this is an image where every pixel is white if it matches the target color, black otherwise
mask = ImageChops.difference(img, Image.new("RGB", img.size, target_color))
Convert mask to grayscale, then invert it so matching pixels are white, non-matching are black
mask = mask.convert("L")
mask = ImageChops.invert(mask)
Now, apply the new color only where the mask is white (the target pixels)
First, make a new solid color image the size of the original
new_color_img = Image.new("RGB", img.size, new_color)
Composite the original image and the new color image using the mask
result = Image.composite(new_color_img, img, mask)
That’s it! This method is like 10x faster than looping through every pixel, especially for big images. I use this all the time when I’m editing my own project photos—last month I had 50 product shots that needed their black buttons switched to red, and this method knocked that job down from an hour to 5 minutes. As your Pillow supplier, we always recommend keeping these two methods in your back pocket: the loop method for small, precise edits, and the ImageChops method for bulk or large images.
Now, let’s talk about common mistakes people make (we’ve all been there). First, not converting to the right mode. If you leave an image in palette mode, your color values won’t match what you pick with the color picker, and your edit will look like a hot mess. Always convert to RGB or RGBA first, I beg you. Second, setting the tolerance too high or too low. If your edit looks splotchy, lower the tolerance. If you’re changing stuff you didn’t mean to, bump it down. Third, not saving the image correctly. When you save, make sure to use the right format—if you saved a PNG as a JPG, you’ll lose transparency and get compression artifacts that mess up your edit. So use result.save("edited_dog.png") for PNGs, and result.save("edited_dog.jpg", quality=95) for JPGs (quality 95 keeps it sharp, no blurriness).
Another thing: what if you want to replace a range of colors, not just one specific shade? Like, all the reds in an image, from bright cherry red to dark maroon. You can adjust the tolerance to cover that, or use a more complex condition. For example, if you want to replace any red with a green, you could target all pixels where the red channel is higher than 150, green is lower than 100, and blue is lower than 100. That works too—Pillow is flexible like that.
Now, a quick shoutout to us—your Pillow supplier. We don’t just sell the library, we know it inside and out, because that’s our job. We get that you don’t have time to dig through 100 Stack Overflow posts to figure out why your color edit isn’t working, so we’ve put together a quick cheat sheet for all these methods on our site (wait, no, I can’t link it—rules, right? But you can reach out to our team if you need help with any Pillow stuff, including color replacement, or if you want bulk licenses for Pillow for your business). We also stock premium Pillow packages for enterprises that need support for large-scale image processing, so you don’t have to worry about crashes or slowdowns when you’re processing hundreds of images.
Wait, let’s test this code with a real example so you know it works. Last week, a customer of ours came to us with a photo of their new water bottle—they had a teal logo that clashed with the mint green bottle, and they wanted to make the logo purple. They tried 3 different apps that ruined the image quality, then found our Pillow resources. We walked them through the loop method with a tolerance of 15, and in 2 minutes they had the exact edit they wanted. No quality loss, no weird artifacts. That’s the power of Pillow.
Another use case: I edited a photo for a local food blog last month. The blogger had a picture of a chocolate cake, but the frosting was this weird sickly yellow color from the lighting. I used the ImageChops method to replace all the yellow frosting with a nice pale cream, and it looked like the original photo had been shot with perfect lighting. No one could tell it was edited.
So to wrap this up, replacing a color in an image with Pillow is super simple once you know the two main methods. For small, precise edits, use the pixel loop method with a tolerance. For fast, bulk or large images, use the ImageChops composite method. Just remember to convert your image to the right mode, get your RGB values right, and save it in the correct format.

If you’re looking to scale this—like automating color replacement for hundreds of product images, or using Pillow for other image processing tasks like resizing or adding filters—our team can help. We’re here to answer all your Pillow questions, hook you up with the right versions for your project, and make sure your image workflows run smoothly. Just reach out to our procurement team to discuss your needs, whether you’re a small business needing a few licenses or a big enterprise needing enterprise-level Pillow support. You won’t regret it.
Lacrosse Jersey References
Pillow Documentation. (n.d.). Image Module. Retrieved from official Pillow docs
Pillow Documentation. (n.d.). ImageChops Module. Retrieved from official Pillow docs
Stack Overflow Community. (Various). Common color replacement techniques with Pillow. Retrieved from public Stack Overflow threads
Tonton Sportswear (Shenzhen) Co., Ltd.
Tonton Sportswear (Shenzhen) Co., Ltd. is one of the leading China pillow manufacturers and suppliers, with our professional factory, we are able to produce custom and pillow at good price. If you are looking for cheap, or you want to wholesale products, please feel free to contact us.
Address: 1-3/F, No.1 Building, Jiewei Industrial Park, No.53, Pingxin North Rd, Pinghu, Longgang District, Shenzhen 518111, China
E-mail: dean@sztonton.com
WebSite: https://www.tontonsportswear.com/