CodeGym /Courses /Python SELF EN /Working with Colors and Image Correction

Working with Colors and Image Correction

Python SELF EN
Level 45, Lesson 4
Available

1. Changing Color Modes

An image can be represented in various color modes, such as:

  • RGB: Standard color mode.
  • L: Grayscale.
  • CMYK: Used in printing.
  • RGBA: RGB with an alpha channel (for transparency).

You can convert an image to a different color mode using the convert() method.

Converting to Grayscale

To make an image black and white, use the L color mode:


# Convert to grayscale
gray_image = image.convert("L")
gray_image.save("gray_example.jpg")

This code converts an image to black and white and saves it to the file gray_example.jpg. This is useful if you want a black-and-white style or need to prepare the image for analysis where only brightness levels matter.

Converting to CMYK Mode

CMYK is often used in printing. Converting an image to this mode can be useful if the image is intended for print.


# Convert to CMYK
cmyk_image = image.convert("CMYK")
cmyk_image.save("cmyk_example.jpg")

2. Applying Filters to an Image

Pillow provides several built-in filters that can be used to create various effects. These filters are in the ImageFilter module.

Applying Blur, Contour, and Sharpen Filters


from PIL import ImageFilter

# Apply blur filter
blurred_image = image.filter(ImageFilter.BLUR)
blurred_image.save("blurred_example.jpg")

# Apply contour filter
contour_image = image.filter(ImageFilter.CONTOUR)
contour_image.save("contour_example.jpg")

# Apply sharpen filter
sharpened_image = image.filter(ImageFilter.SHARPEN)
sharpened_image.save("sharpened_example.jpg")

Here, we apply three different filters:

  • BLUR: Blurs the image, which is useful for creating soft effects.
  • CONTOUR: Highlights the contours of objects in the image.
  • SHARPEN: Enhances sharpness, making the image clearer.

Other Filters

Some other useful filters in Pillow:

  • DETAIL: Enhances details.
  • EDGE_ENHANCE: Enhances edges.
  • SMOOTH: Smoothens the image.

# Apply edge enhance filter
edge_image = image.filter(ImageFilter.EDGE_ENHANCE)
edge_image.save("edge_enhance_example.jpg")

3. Adjusting Brightness, Contrast, Sharpness, and Saturation

The ImageEnhance package in Pillow makes it easy to modify brightness, contrast, sharpness, and color saturation of an image.

Adjusting Brightness

To adjust brightness, use the ImageEnhance.Brightness class. The brightness factor can be any number: 1.0 means original brightness, less than 1.0 darkens, and more than 1.0 brightens the image.


from PIL import ImageEnhance

# Adjust brightness
enhancer = ImageEnhance.Brightness(image)
brighter_image = enhancer.enhance(1.5)  # Increase brightness by 50%
brighter_image.save("brighter_example.jpg")

Adjusting Contrast

Contrast makes highlights brighter and shadows darker. Use ImageEnhance.Contrast to adjust contrast.


# Adjust contrast
enhancer = ImageEnhance.Contrast(image)
higher_contrast_image = enhancer.enhance(1.5)  # Increase contrast by 50%
higher_contrast_image.save("higher_contrast_example.jpg")

Adjusting Sharpness

Sharpness determines how crisp the edges of objects in an image will look. Use ImageEnhance.Sharpness to adjust sharpness.


# Adjust sharpness
enhancer = ImageEnhance.Sharpness(image)
sharper_image = enhancer.enhance(2.0)  # Double the sharpness
sharper_image.save("sharper_example.jpg")

A value of 1.0 keeps the original sharpness, less than 1.0 makes the image blurrier, and more than 1.0 makes it sharper.

Adjusting Color Saturation

Color saturation makes colors more or less intense. Use ImageEnhance.Color to adjust saturation.


# Adjust saturation
enhancer = ImageEnhance.Color(image)
more_vibrant_image = enhancer.enhance(1.5)  # Increase saturation by 50%
more_vibrant_image.save("more_vibrant_example.jpg")

4. Comparing Image Correction Methods

Method Description Application
convert("L") Converts image to black-and-white mode Black-and-white style, tone analysis
ImageFilter Applies filters for blur, sharpness, etc. Overlay effects, object highlighting
ImageEnhance.Brightness Adjusts image brightness Brightening or darkening
ImageEnhance.Contrast Adjusts contrast Improving detail visibility
ImageEnhance.Sharpness Adjusts sharpness Highlighting edges, increasing clarity
ImageEnhance.Color Adjusts color saturation Creating vibrant, saturated images

5. Examples

Example of Full Image Processing

Let's combine everything mentioned above and write code that performs several stages of color correction, applying filters, and adjusting brightness, contrast, and saturation.


from PIL import Image, ImageEnhance, ImageFilter

# Load the image
image = Image.open("example.jpg")

# Convert to grayscale
gray_image = image.convert("L")
gray_image.save("gray_example.jpg")

# Apply sharpen filter
sharpened_image = gray_image.filter(ImageFilter.SHARPEN)
sharpened_image.save("sharpened_gray_example.jpg")

# Increase brightness
enhancer = ImageEnhance.Brightness(sharpened_image)
brighter_image = enhancer.enhance(1.3)  # 30% brighter
brighter_image.save("brighter_gray_example.jpg")

# Increase contrast
enhancer = ImageEnhance.Contrast(brighter_image)
higher_contrast_image = enhancer.enhance(1.5)  # 50% more contrast
higher_contrast_image.save("final_example.jpg")

Practical Applications of Color Correction

  • Photo Editing: Adjusting brightness and contrast can enhance photo details, and filters can make the image more expressive.
  • Preparing Images for Analysis: Converting to black and white, increasing sharpness and contrast can make the image clearer and suitable for analysis.
  • Image Styling: Changing saturation and applying filters allows creating unique styles and effects for design projects.
1
Task
Python SELF EN, level 45, lesson 4
Locked
Image Conversion to Different Color Modes
Image Conversion to Different Color Modes
2
Task
Python SELF EN, level 45, lesson 4
Locked
Applying Filters to an Image
Applying Filters to an Image
3
Task
Python SELF EN, level 45, lesson 4
Locked
Brightness, Contrast, and Saturation Adjustment
Brightness, Contrast, and Saturation Adjustment
4
Task
Python SELF EN, level 45, lesson 4
Locked
Comprehensive Image Processing and Styling
Comprehensive Image Processing and Styling
1
Survey/quiz
Working with images using Pillow, level 45, lesson 4
Unavailable
Working with images using Pillow
Working with images using Pillow
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION