In [1]:
import imageio.v2 as imageio
import numpy as np
import matplotlib.pyplot as plt
def plot_image_from_array(image_array, title=''''Image''''):
plt.imshow(image_array, cmap=''''gray'''')
plt.axis(''''off'''') # Hide axes
plt.title(title)
plt.show()
In [2]:
from PIL import Image, ImageFilter
im = Image.open("lena.jpg")
In [3]:
# Apply a MinFilter to the image
im_min = im.filter(ImageFilter.MinFilter(3))
plot_image_from_array(im_min, title=''''MinFilter'''')
In [4]:
# Apply a MaxFilter to the image
im_max = im.filter(ImageFilter.MaxFilter(3))
plot_image_from_array(im_max, title=''''MaxFilter'''')
In [5]:
# Apply a MedianFilter to the image
im_median = im.filter(ImageFilter.MedianFilter(3))
plot_image_from_array(im_median, title=''''MedianFilter'''')
In [6]:
# Apply a ModeFilter to the image
im_mode = im.filter(ImageFilter.ModeFilter(3))
plot_image_from_array(im_mode, title=''''ModeFilter'''')
In [7]:
# Apply a custom Kernel filter to the image
kernel = ImageFilter.Kernel((3, 3), [0, -1, 0, -1, 5, -1, 0, -1, 0])
im_kernel = im.filter(kernel)
plot_image_from_array(im_kernel, title=''''kernel'''')