Applying MaxFilter on an image using Pillow library

In this program, we will apply a maximum filter on an image using the Pillow library. In maximum filtering, the value of each pixel in a selected window of the image is replaced by the maximum pixel value of that window. This creates a dilation effect that brightens the image and expands bright regions.

What is MaxFilter?

The MaxFilter is a morphological operation that replaces each pixel with the maximum value found in its neighborhood window. It's commonly used for noise removal and feature enhancement in image processing.

Syntax

ImageFilter.MaxFilter(size)

Parameters:

  • size ? Size of the filter window (default is 3). Must be an odd number.

Example

Let's apply a maximum filter to an image ?

from PIL import Image, ImageFilter
import matplotlib.pyplot as plt
import numpy as np

# Create a sample image with some noise
img_array = np.zeros((100, 100), dtype=np.uint8)
img_array[20:80, 20:80] = 128  # Gray rectangle
img_array[40:60, 40:60] = 255  # White square in center

# Add some noise points
img_array[25, 25] = 255
img_array[70, 30] = 255
img_array[35, 70] = 255

# Convert to PIL Image
original_img = Image.fromarray(img_array, mode='L')

# Apply MaxFilter
filtered_img = original_img.filter(ImageFilter.MaxFilter(size=5))

print("MaxFilter applied successfully!")
print(f"Original image size: {original_img.size}")
print(f"Filter window size: 5x5")
MaxFilter applied successfully!
Original image size: (100, 100)
Filter window size: 5x5

Comparison with Different Window Sizes

Let's see how different window sizes affect the filtering ?

from PIL import Image, ImageFilter
import numpy as np

# Create a test image
img_array = np.zeros((50, 50), dtype=np.uint8)
img_array[10:40, 10:40] = 100
img_array[20:30, 20:30] = 255

original_img = Image.fromarray(img_array, mode='L')

# Apply different filter sizes
sizes = [3, 5, 7]
results = {}

for size in sizes:
    filtered = original_img.filter(ImageFilter.MaxFilter(size=size))
    results[size] = filtered
    print(f"MaxFilter with size {size} applied")

print("All filters applied successfully!")
MaxFilter with size 3 applied
MaxFilter with size 5 applied
MaxFilter with size 7 applied
All filters applied successfully!

Comparison Table

Window Size Effect Processing Time Best Use Case
3x3 Subtle brightening Fast Light noise removal
5x5 Moderate dilation Medium General enhancement
7x7 Strong dilation Slower Heavy noise or feature expansion

Complete Example with File

from PIL import Image, ImageFilter

# Open an image file
original_image = Image.open('input_image.jpg')

# Apply MaxFilter
filtered_image = original_image.filter(ImageFilter.MaxFilter(size=7))

# Save the result
filtered_image.save('max_filtered_output.jpg')

# Display both images
original_image.show(title="Original Image")
filtered_image.show(title="Max Filtered Image")

print("MaxFilter applied and saved successfully!")

Key Points

  • MaxFilter expands bright regions and can remove dark noise
  • Larger window sizes create stronger dilation effects
  • Window size must be an odd number (3, 5, 7, etc.)
  • Useful for morphological operations and image enhancement

Conclusion

The MaxFilter in Pillow is effective for image enhancement and noise removal. It replaces each pixel with the maximum value in its neighborhood, creating a brightening and dilation effect that's useful in various image processing applications.

---
Updated on: 2026-03-25T18:07:02+05:30

583 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements