Image Filters and Enhancements in Pillow
Pillow provides powerful tools to enhance and modify images using built-in filters and adjustments for brightness, contrast, and sharpness. These features allow for creative and practical image processing.
Using Built-in Filters
The ImageFilter
module in Pillow provides several pre-defined filters that can be applied to images for artistic and functional effects.
Common Filters
Filter | Description |
---|---|
BLUR | Applies a blur effect to the image. |
CONTOUR | Highlights the edges of objects. |
DETAIL | Enhances the details in the image. |
EDGE_ENHANCE | Emphasizes the edges in the image. |
SHARPEN | Sharpens the image. |
Example: Applying Filters
from PIL import Image, ImageFilter
# Open an image
img = Image.open("example.jpg")
# Apply BLUR filter
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.show()
# Apply CONTOUR filter
contoured_img = img.filter(ImageFilter.CONTOUR)
contoured_img.show()
# Apply SHARPEN filter
sharpened_img = img.filter(ImageFilter.SHARPEN)
sharpened_img.show()
Adjusting Brightness, Contrast, and Sharpness
The ImageEnhance
module allows you to adjust the brightness, contrast, sharpness, and color of an image.
Brightness Adjustment
Use ImageEnhance.Brightness
to modify brightness levels.
from PIL import Image, ImageEnhance
# Open an image
img = Image.open("example.jpg")
# Enhance brightness
enhancer = ImageEnhance.Brightness(img)
bright_img = enhancer.enhance(1.5) # Increase brightness by 50%
bright_img.show()
Contrast Adjustment
Use ImageEnhance.Contrast
to adjust contrast.
# Enhance contrast
enhancer = ImageEnhance.Contrast(img)
high_contrast_img = enhancer.enhance(2.0) # Double the contrast
high_contrast_img.show()
Sharpness Adjustment
Use ImageEnhance.Sharpness
to control sharpness.
# Enhance sharpness
enhancer = ImageEnhance.Sharpness(img)
sharp_img = enhancer.enhance(3.0) # Triple the sharpness
sharp_img.show()
Combining Filters and Enhancements
Filters and enhancements can be combined to achieve more complex effects.
Example: Create a Unique Effect
# Apply SHARPEN filter
sharpened_img = img.filter(ImageFilter.SHARPEN)
# Adjust brightness
enhancer = ImageEnhance.Brightness(sharpened_img)
bright_sharpened_img = enhancer.enhance(1.8)
bright_sharpened_img.show()
Try It Yourself
Problem 1: Apply Multiple Filters
Write a script to apply the DETAIL
and EDGE_ENHANCE
filters to an image named sample.jpg
, and save the output as enhanced_sample.jpg
.
Show Solution
from PIL import Image, ImageFilter
img = Image.open("sample.jpg")
detailed_img = img.filter(ImageFilter.DETAIL)
edge_img = detailed_img.filter(ImageFilter.EDGE_ENHANCE)
edge_img.save("enhanced_sample.jpg")
edge_img.show()
Problem 2: Adjust Brightness and Contrast
Write a script to increase the brightness by 30% and double the contrast of an image named sample.jpg
.
Show Solution
from PIL import Image, ImageEnhance
img = Image.open("sample.jpg")
# Adjust brightness
brightness_enhancer = ImageEnhance.Brightness(img)
bright_img = brightness_enhancer.enhance(1.3) # Increase brightness by 30%
# Adjust contrast
contrast_enhancer = ImageEnhance.Contrast(bright_img)
enhanced_img = contrast_enhancer.enhance(2.0) # Double the contrast
enhanced_img.show()
Enhancing images with filters and adjustments can make them more visually appealing and suitable for various applications. Continue exploring these features to master image manipulation with Pillow!