If you’ve ever worked with image processing, you’ve probably found yourself in a spot where you need to isolate a single color—say, the bright red of a product logo from a product shot, or the soft blue of a brand accent element from a marketing graphic. For Python users, Pillow is the go-to library for this kind of work, and as a long-time supplier of Pillow packages to developers and small-scale dev teams, I’ve fielded dozens of questions about how to pull off this specific color extraction without wasting hours fumbling with complex tools. This isn’t magic, just straightforward, repeatable steps that work for most common image types, and I’m breaking it down here the same way I walk new clients through it when they order Pillow from me. Pillow

First, let’s clear up a quick point: when we talk about extracting a specific color from an image, we’re not just picking a random pixel’s hex code—we’re targeting all pixels that fall within a narrow range of that color’s hue, saturation, and brightness values, accounting for minor lighting shifts, compression artifacts, or edge blur. Pillow makes this accessible because it handles both RGB (the standard for most digital images) and HSV (Hue, Saturation, Value, a color model that’s way easier to work with when you want to target a specific color range) natively, no extra plugins required. That’s part of why my customers keep coming back for Pillow—its core functionality is built for exactly these sorts of real-world tasks, not just niche enterprise use cases.
Let’s start with the basics you’ll need to set up, if you haven’t already. First, install Pillow via pip. You’d be surprised how many people skip this step and waste time with broken, outdated packages—something I hear about all the time. For most users, a simple pip install pillow works, but if you’re working with large, high-resolution images, I recommend grabbing the precompiled binary wheels I stock for Pillow, since they cut down on installation time and avoid common compilation errors on Windows and macOS. Once you have Pillow installed, the first step in any color extraction workflow is loading your image correctly. Open it with Image.open()—easy, but pro tip: always convert your image to RGB mode right after loading, even if it’s a PNG or JPG that looks like it’s already in color. Some image formats (like grayscale or CMYK, common for print work) will throw off your color values, so converting to standard RGB ensures consistency.
Next, the critical part: converting your image to HSV. This is where most people stumble, because if you stick with RGB, you’ll be trying to filter red pixels that are slightly darker or brighter than your target, and you’ll accidentally cut out too much or keep too much. HSV breaks colors into three separate, intuitive parts: Hue (the actual color on a 0–360 degree wheel, where red is 0°, green is 120°, blue is 240°), Saturation (how “vivid” the color is, from 0 for gray to 255 for pure color), and Value (brightness, from 0 for black to 255 for pure white). To convert an RGB image to HSV in Pillow, you’ll use the convert('HSV') method on your Image object. This gives you a three-channel HSV array, which is perfect for defining your target color range.
Let’s use a concrete example to make this tangible. Suppose you have a product image with a bright brand red, and you want to extract all red elements from it. First, pick your target hue value: pure red is 0 in HSV, but most real-world reds will be between 0 and 10 degrees, to account for slight lighting shifts. Next, set a saturation range: if you only want vivid, true red (not washed-out pinks or faded tones), you might set the lower saturation to 150, so any pixel that’s too gray (saturation below 150) gets excluded. Then for brightness, set a range from 50 (so you don’t pick up dark black edges) up to 255 (so you get the brightest red highlights). This gives you a lower bound of (0, 150, 50) and an upper bound of (10, 255, 255) for your red range. These numbers aren’t set in stone—adjust based on your image’s lighting and how precise you need the extraction to be.
Now, to turn that range into a mask, Pillow’s ImageOps and numpy (we use numpy alongside Pillow for this step in almost all our enterprise supply orders, since it’s lightweight and plays perfectly with Pillow’s image arrays) make this simple. First, convert your HSV image to a numpy array, so you can do element-wise comparisons. Then, create a mask array where each pixel is True if it falls within your HSV range, and False if it doesn’t. If you’re not familiar with numpy, don’t overcomplicate it—this is just a way to check every pixel against your target values in one line, instead of looping through each pixel manually. Once you have your mask, you can use it to isolate your color: either apply it to the original image to keep only the target color, or invert it to keep everything else.
Wait, let’s talk about a common pitfall here: some users try to use RGB ranges instead of HSV, and they end up with a lopsided extraction. For example, pure red in RGB is (255, 0, 0), but a dark red might be (150, 0, 0) and a light red might be (255, 100, 100). If you set an RGB range for red, you’ll accidentally pick up pinks that are too light or browns that are too dark, because RGB mixes all color channels together. HSV solves this by separating color identity (hue) from its intensity and vividness, which makes it far more reliable for targeted color work. That’s why I always advise new Pillow users to practice with HSV first—once you get the hang of adjusting hue, saturation, and value ranges, it’s way less trial and error than guessing at RGB values.
Another step that often gets overlooked: handling anti-aliased edges. If your image has smooth, blurred edges around your target color (like a logo on a white background), the edge pixels won’t be pure red—they’ll be semi-transparent or have a mix of your target color and the background. If you just use a hard mask, you’ll end up with jagged, pixelated edges. To fix this, you can slightly expand your HSV range by a few degrees of hue, and then use a small blur on your mask, or use the alpha_composite method in Pillow to blend the mask edges smoothly. I include a short example of this in the documentation I send with every Pillow order, because it’s a tiny tweak that makes extracted colors look professional, not like a bot did the work.
Let’s test this workflow with a real code snippet, the same one I walk customers through when they’re setting up their first color extraction script. First, load your image, convert it to RGB, then to HSV:
from PIL import Image
import numpy as np
Load your image (I always recommend using image files from local storage, not URLs, for consistency)
img = Image.open("product_shot.jpg").convert("RGB")
Convert to HSV for color targeting
hsv_img = img.convert("HSV")
hsv_array = np.array(hsv_img)
Define your HSV range for bright red—adjust these values to match your target color
lower_red = np.array([0, 150, 50])
upper_red = np.array([10, 255, 255])
Create the mask: True for pixels within your range, False otherwise
mask = np.all((hsv_array >= lower_red) & (hsv_array <= upper_red), axis=2)
Convert the mask back to a Pillow Image, then use it to isolate the color
mask_img = Image.fromarray((mask * 255).astype(np.uint8))
extracted_color = Image.composite(img, Image.new("RGB", img.size, (255, 255, 255)), mask_img)
Save your result
extracted_color.save("red_extract.png")
That’s it—no fancy tools, just Pillow and numpy, both of which I supply to dev teams for exactly this kind of project. Now, let’s go over why this works, and where people usually mess it up. First, if your extraction is leaving out parts of the target color, your HSV range is too narrow—widen the upper and lower hue bounds. If it’s picking up too much extra color, shrink the saturation or value range. If your image is a PNG with transparency, just make sure to load it in RGBA mode first, convert that to HSV, and the mask will work the same way.
I’ve also noticed that a lot of developers confuse extracting a color with changing a color’s saturation, which is a different task—this workflow is specifically for isolating one color, not modifying it. That’s a key distinction, and it’s why I make sure to clarify it in all my Pillow supply communications. If someone is asking to change all red pixels to blue, that’s a simple color replacement, but extracting means making those red pixels the only visible ones, or turning the rest of the image transparent.
For more complex images, like photos with multiple colors or uneven lighting, you might need to use a second mask to account for shadows or highlights. For example, if your red logo has a darker shaded area at the bottom, you can add a range of lower brightness values to your HSV upper and lower bounds so that the shaded parts are included. This is where testing small adjustments to your HSV values comes in—spend 10 minutes tweaking the range on a small test image, and you’ll save hours of reworking a final product.
Now, as a supplier of Pillow, I can’t stress enough how reliable this library is for this kind of work. A lot of open-source image libraries have buggy color conversion or poor performance with large images, but Pillow is maintained consistently, and its color mode handling is accurate across all common image formats. I’ve supplied Pillow to teams that use color extraction for e-commerce product catalogs, graphic design, and even medical imaging—this workflow is flexible enough for all those use cases.
If you’re just getting started, I recommend testing this with a sample image that has a clear, single target color first, before moving to more complex shots. Play with different hue ranges: if you’re extracting blue, your hue range will be around 220 to 240, and green is around 100 to 140. Saturation and value ranges will stay similar, but the hue is the key number that changes based on your target color.

At the end of the day, extracting a specific color from an image with Pillow isn’t about mastering complicated algorithms—it’s about understanding how HSV color space works, and using Pillow’s built-in functions to turn that understanding into a usable mask. If you’re working on a project that requires this kind of image processing, or you need a reliable supply of Pillow packages to streamline your workflow, feel free to reach out to discuss your specific needs. We can help you get set up with the right version of Pillow, troubleshoot any color conversion issues, or even adapt this workflow to fit your exact image processing tasks.
Cotton Quilt REFERENCES
Pillow Documentation: Image.convert() Method
Pillow Documentation: Image.composite() Method
OpenCV Documentation: HSV Color Space Basics (for context, though we use Pillow here)
Python Software Foundation: NumPy Array Manipulation Guide
Hangzhou Tangjin Textile Co., Ltd.
Hangzhou Tangjin Textile Co., Ltd. is one of the most professional pillow manufacturers and suppliers in China, featured by quality products and good service. Please rest assured to wholesale custom made pillow at competitive price from our factory.
Address: No.55, Louying Road, Louta Town, Xiaoshan District, Hangzhou City, Zhejiang Province
E-mail: wendy@tangjintex.com
WebSite: https://www.tangjin-hometex.com/