[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Brighten an image's saturated pixels with Python and PIL

[This image's blue pixels have been brightened]

Sometimes due to poor lighting, a picture's colors come out a bit off. This program lets you adjust the brightness of pixels that are mostly red, green, or blue.

Using the Program

Use the File menu's Open command to load an image. Use the radio buttons to select red, green, or blue pixels. When you adjust the scale widget (which the non-tkinter world calls a slider), the program finds pixels where the selected color component is at least the threshold value greater than the other components.

For example, in the picture, the red color component is selected and the threshold is 32. If a pixel's red component is at 32 greater than the green and blue components, then the program brightens it.

After you've achieved a result that you like, use the File menu's Save As command to save the result.

The brighten_saturated_pixels Method

The key to this program is the following brighten_saturated_pixels method.

def brighten_saturated_pixels(image, threshold, scale, component): '''Brighten saturated pixels.''' # The component parameter is 0=red, 1=green, 2=blue. # Make a copy so we don't mess up the original. image = image.copy() # Convert to RGB if necessary. if image.mode != 'RGB': image = image.convert('RGB') # Process the copy's pixels. pixels = image.load() for y in range(image.height): for x in range(image.width): (r, g, b) = pixels[x, y] if component == 0: change_it = r >= g + threshold and r >= b + threshold elif component == 1: change_it = g >= r + threshold and g >= b + threshold elif component == 2: change_it = b >= r + threshold and b >= g + threshold if change_it: r = int(min(255, r * scale)) g = int(min(255, g * scale)) b = int(min(255, b * scale)) pixels[x, y] = (r, g, b) # Return the result image. return image

The method first makes a copy of the image so it doesn't mess up the original. It then converts the image to the RGB format (in case it isn't already in that format). (If you want to work with some other format like RGBA, you'll have to modify the code.)

Next, the code loads the image's pixels so it can examine and modify them more easily. It then loops through the pixels.

The code gets each pixel's red, green, and blue components. Then, depending on the selected color component, the code determines whether the pixel meets the selection requirements. (For example, blue >= 32 + red and blue >= 32 + green.)

If the pixel meets the selection requirements, the program multiplies the pixel's color component values by the scale factor that you selected with the Scale widget. It uses the min function to keep the new value between 0 and 255.

After it has updated the pixel's color components, the program updates the pixel in the image.

Note that there are other ways you could brighten the pixel. For example, you could scale the three component values by different amounts or you could scale the selected component by a larger amount.

Also note that you can darken pixels by setting the brightness scale to a value less than 1.

The current program lets you set the scale to a maximum of 5.0, but in most cases you'll only want to brighten pixels by a factor of 1.2 or so. In the picture above you can see that the blue pixels have been brightened too much to look good.

Conclusion

This example performs a single limited task, but I've found it very useful over the years. Download the example to experiment with it.
 
For more information image processing in Python, see my Manning liveProject Algorithm Projects with Python: Image Processing. It explains how to do things like:
• Rotation• Scaling• Stretching
• Brightness enhancement• Contrast enhancement• Cropping
• Remapping colors• Image sharpening• Embossing
• Color enhancement• Sharpening• Gray scale
• Black and white• Sepia tone• Other color scales
 
© 2024 - 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.