{"id":3473,"date":"2026-10-08T20:57:32","date_gmt":"2026-10-08T12:57:32","guid":{"rendered":"http:\/\/www.bhaktigyan.com\/blog\/?p=3473"},"modified":"2026-10-08T20:57:32","modified_gmt":"2026-10-08T12:57:32","slug":"how-to-replace-a-color-in-an-image-with-pillow-4113-c64853","status":"publish","type":"post","link":"http:\/\/www.bhaktigyan.com\/blog\/2026\/10\/08\/how-to-replace-a-color-in-an-image-with-pillow-4113-c64853\/","title":{"rendered":"How to replace a color in an image with Pillow?"},"content":{"rendered":"<p>Hey everyone, it\u2019s Jake from your go-to Pillow supplier\u2014you know, the folks who hook you up with all the top-tier Python imaging tools you actually need, no hassle. Today we\u2019re talking about something that\u2019s 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\u2014whether you\u2019re 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\u2019re using Pillow, you\u2019ve already got the perfect tool for the job\u2014no need to download sketchy random apps or pay for overpriced editing software. Let\u2019s break this down like we\u2019re chatting over a coffee, no jargon, just real steps that work. <a href=\"https:\/\/www.tontonsportswear.com\/hockey-jerseys-1\">Pillow<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.tontonsportswear.com\/uploads\/20187147\/small\/201810191105527674389.jpg\"><\/p>\n<p>First off, a quick heads up: if you don\u2019t already have Pillow installed, that\u2019s the first tiny step. Most of you probably know this, but it\u2019s easy to mix up package names (I\u2019ve been there\u2014once I tried installing \u201cpillow\u201d with a typo and wasted 10 minutes). So open your terminal or command prompt, type <code>pip install pillow<\/code>, and you\u2019re golden. If you\u2019re on a Mac or Linux, add <code>--user<\/code> at the end if you get a permission error, and boom\u2014you\u2019re ready to roll.<\/p>\n<p>Now, why are we hyped about Pillow for this? It\u2019s lightweight, fast, and since it\u2019s a Python library, you can even automate this process if you\u2019ve 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\u2019t run into weird bugs\u2014trust me, there\u2019s nothing worse than half-finished color edits because you\u2019re on an old, glitchy release.<\/p>\n<p>Let\u2019s get to the actual code, because that\u2019s what you\u2019re here for. First, you need to load your image. Let\u2019s say we\u2019ve got a sample image named <code>dog.jpeg<\/code>\u2014it\u2019s a cute golden retriever, but that bright blue collar he\u2019s 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\u2019s how to start:<\/p>\n<p>from PIL import Image<br \/>\nfrom PIL.Image import Color<\/p>\n<h1>Load the image<\/h1>\n<p>img = Image.open(&quot;dog.jpeg&quot;)<\/p>\n<h1>Convert to RGB mode to make color picking easier (no weird transparency or grayscale mix-ups)<\/h1>\n<p>img = img.convert(&quot;RGB&quot;)<\/p>\n<p>Wait, hold up\u2014why 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\u2019re targeting.<\/p>\n<p>Next step: pick the color you want to replace. This is the most important part\u2014if you don\u2019t 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\u2019s say the RGB values are (25, 100, 220). Got it. Now, the color we want to replace it with\u2014let\u2019s say that\u2019s (255, 150, 50), that warm orange.<\/p>\n<p>Now, here\u2019s 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\u2019s write that code:<\/p>\n<h1>Target color we want to replace (that blue collar)<\/h1>\n<p>target_color = (25, 100, 220)<\/p>\n<h1>New color to swap in<\/h1>\n<p>new_color = (255, 150, 50)<\/p>\n<h1>Get the pixel data as a list so we can modify it<\/h1>\n<p>pixels = img.load()<\/p>\n<h1>Loop through every x and y coordinate in the image<\/h1>\n<p>for x in range(img.width):<br \/>\nfor y in range(img.height):<br \/>\n# Get the color of the current pixel<br \/>\ncurrent_color = pixels[x, y]<br \/>\n# If it&#8217;s exactly the target color, replace it<br \/>\nif current_color == target_color:<br \/>\npixels[x, y] = new_color<\/p>\n<p>Wait a second\u2014what if the color isn\u2019t perfect? Like, maybe the blue collar has a tiny shade variation, not exactly (25,100,220). If you use exact matching, you\u2019ll miss those areas, and the edit will look patchy. That\u2019s a common mistake, so let\u2019s 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\u2019ll replace it. That way, subtle shade differences don\u2019t break the edit. Here\u2019s how to adjust that part:<\/p>\n<h1>Tolerance value\u2014play with this! 10-30 works for most cases<\/h1>\n<p>tolerance = 20<br \/>\nif (abs(current_color[0] &#8211; target_color[0]) &lt; tolerance and<br \/>\nabs(current_color[1] &#8211; target_color[1]) &lt; tolerance and<br \/>\nabs(current_color[2] &#8211; target_color[2]) &lt; tolerance):<br \/>\npixels[x, y] = new_color<\/p>\n<p>Oh, that\u2019s way better! I learned this trick when I was editing product photos for a local bakery a while back\u2014their 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\u2019re 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\u2019t go over 30, though\u2014you might accidentally change parts of the image that weren\u2019t supposed to be touched, like the blue sky in the background of a landscape shot.<\/p>\n<p>Now, what about images with transparent backgrounds? Like PNGs with a see-through area. The code above works, but let\u2019s adjust it to handle RGBA mode so you don\u2019t mess up the transparency. Let\u2019s rewrite the load part to keep alpha:<\/p>\n<p>img = Image.open(&quot;dog.png&quot;)<\/p>\n<h1>Keep RGBA mode instead of converting to RGB, so transparency stays intact<\/h1>\n<p>img = img.convert(&quot;RGBA&quot;)<\/p>\n<h1>Now, target color will have an alpha value too\u2014let&#8217;s say our blue collar has alpha 255 (fully opaque)<\/h1>\n<p>target_color = (25, 100, 220, 255)<br \/>\nnew_color = (255, 150, 50, 255)<br \/>\ntolerance = 20<\/p>\n<p>And the pixel check stays almost the same\u2014just 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 <code>abs(current_color[3] - target_color[3]) &lt; tolerance<\/code> to the condition. Easy peasy.<\/p>\n<p>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\u2019ve waited 5 minutes for a 10MP image to process once\u2014total buzzkill). That\u2019s where a Pillow trick with <code>ImageChops<\/code> comes in, which is way faster because it\u2019s built on optimized C code, not Python loops. Let\u2019s go over that method too, since a lot of folks ask about speed.<\/p>\n<p>First, import ImageChops from Pillow:<\/p>\n<p>from PIL import Image, ImageChops<\/p>\n<h1>Load your image, same as before<\/h1>\n<p>img = Image.open(&quot;city_building.jpg&quot;).convert(&quot;RGB&quot;)<br \/>\ntarget_color = (180, 200, 220) # that light gray window color<br \/>\nnew_color = (50, 100, 150) # deep blue for the windows<\/p>\n<h1>Make a mask: this is an image where every pixel is white if it matches the target color, black otherwise<\/h1>\n<p>mask = ImageChops.difference(img, Image.new(&quot;RGB&quot;, img.size, target_color))<\/p>\n<h1>Convert mask to grayscale, then invert it so matching pixels are white, non-matching are black<\/h1>\n<p>mask = mask.convert(&quot;L&quot;)<br \/>\nmask = ImageChops.invert(mask)<\/p>\n<h1>Now, apply the new color only where the mask is white (the target pixels)<\/h1>\n<h1>First, make a new solid color image the size of the original<\/h1>\n<p>new_color_img = Image.new(&quot;RGB&quot;, img.size, new_color)<\/p>\n<h1>Composite the original image and the new color image using the mask<\/h1>\n<p>result = Image.composite(new_color_img, img, mask)<\/p>\n<p>That\u2019s it! This method is like 10x faster than looping through every pixel, especially for big images. I use this all the time when I\u2019m editing my own project photos\u2014last 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.<\/p>\n<p>Now, let\u2019s talk about common mistakes people make (we\u2019ve all been there). First, not converting to the right mode. If you leave an image in palette mode, your color values won\u2019t 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\u2019re changing stuff you didn\u2019t mean to, bump it down. Third, not saving the image correctly. When you save, make sure to use the right format\u2014if you saved a PNG as a JPG, you\u2019ll lose transparency and get compression artifacts that mess up your edit. So use <code>result.save(&quot;edited_dog.png&quot;)<\/code> for PNGs, and <code>result.save(&quot;edited_dog.jpg&quot;, quality=95)<\/code> for JPGs (quality 95 keeps it sharp, no blurriness).<\/p>\n<p>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\u2014Pillow is flexible like that.<\/p>\n<p>Now, a quick shoutout to us\u2014your Pillow supplier. We don\u2019t just sell the library, we know it inside and out, because that\u2019s our job. We get that you don\u2019t have time to dig through 100 Stack Overflow posts to figure out why your color edit isn\u2019t working, so we\u2019ve put together a quick cheat sheet for all these methods on our site (wait, no, I can\u2019t link it\u2014rules, 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\u2019t have to worry about crashes or slowdowns when you\u2019re processing hundreds of images.<\/p>\n<p>Wait, let\u2019s 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\u2014they 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\u2019s the power of Pillow.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.tontonsportswear.com\/uploads\/20237147\/small\/wrestling-singlets-supplier83073253-50ce-406c-9300-fb89be8d4742.jpg\"><\/p>\n<p>If you\u2019re looking to scale this\u2014like automating color replacement for hundreds of product images, or using Pillow for other image processing tasks like resizing or adding filters\u2014our team can help. We\u2019re 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\u2019re a small business needing a few licenses or a big enterprise needing enterprise-level Pillow support. You won\u2019t regret it.<\/p>\n<p><a href=\"https:\/\/www.tontonsportswear.com\/lacrosse-jersey-1\">Lacrosse Jersey<\/a> References<br \/>\nPillow Documentation. (n.d.). Image Module. Retrieved from official Pillow docs<br \/>\nPillow Documentation. (n.d.). ImageChops Module. Retrieved from official Pillow docs<br \/>\nStack Overflow Community. (Various). Common color replacement techniques with Pillow. Retrieved from public Stack Overflow threads<\/p>\n<hr>\n<p><a href=\"https:\/\/www.tontonsportswear.com\/\">Tonton Sportswear (Shenzhen) Co., Ltd.<\/a><br \/>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.<br \/>Address: 1-3\/F, No.1 Building, Jiewei Industrial Park, No.53, Pingxin North Rd, Pinghu, Longgang District, Shenzhen 518111, China<br \/>E-mail: dean@sztonton.com<br \/>WebSite: <a href=\"https:\/\/www.tontonsportswear.com\/\">https:\/\/www.tontonsportswear.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone, it\u2019s Jake from your go-to Pillow supplier\u2014you know, the folks who hook you up &hellip; <a title=\"How to replace a color in an image with Pillow?\" class=\"hm-read-more\" href=\"http:\/\/www.bhaktigyan.com\/blog\/2026\/10\/08\/how-to-replace-a-color-in-an-image-with-pillow-4113-c64853\/\"><span class=\"screen-reader-text\">How to replace a color in an image with Pillow?<\/span>Read more<\/a><\/p>\n","protected":false},"author":24,"featured_media":3473,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3436],"class_list":["post-3473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-pillow-45df-c67c54"],"_links":{"self":[{"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/posts\/3473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/comments?post=3473"}],"version-history":[{"count":0,"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/posts\/3473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/posts\/3473"}],"wp:attachment":[{"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/media?parent=3473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/categories?post=3473"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bhaktigyan.com\/blog\/wp-json\/wp\/v2\/tags?post=3473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}