Ben Chuanlong Du's Blog

It is never too late to learn.

Image Filters in Pillow

Comments

  1. PIL.ImageGrab works on macOS and Windows only.

  2. You can further crop a screenshot image using the method Image.crop.

In [1]:
import numpy as np
from PIL import Image, ImageFilter
In [2]:
img = Image.open("../../home/media/poker/4h.png")
img
Out[2]:

ImageFilter.BLUR

The BLUR filter blurs an image.

In [35]:
img.filter(ImageFilter.BLUR)
Out[35]:

ImageFilter.MinFilter

The MinFilter takes the minimum value of the specified neighborhood for each pixle. The direct effect is to make lines thicker in an image. The MaxFilter is also refer to as erosion.

In [36]:
img.filter(ImageFilter.MinFilter(3))
Out[36]:

Or equivalently,

In [37]:
img.filter(ImageFilter.MinFilter)
Out[37]:

ImageFilter.MaxFilter

The MaxFilter takes the maximum value of the specified neighborhood for each pixle. The direct effect is to make lines thinner in an image. The MaxFilter is also refer to as dilation.

In [3]:
img.filter(ImageFilter.MaxFilter)
Out[3]:

ImageFilter.ModeFilter

The ModeFilter takes the mode value of the specified neighborhood for each pixle. The direct effect is to makes thin lines disappear.

In [4]:
img.filter(ImageFilter.ModeFilter)
Out[4]:

ImageFilter.CONTOUR

The CONTOUR filter gets contours of objects in an image.

In [38]:
img.filter(ImageFilter.CONTOUR)
Out[38]:

ImageFilter.DETAIL

In [39]:
img.filter(ImageFilter.DETAIL)
Out[39]:

ImageFilter.EDGE_ENHANCE

In [40]:
img.filter(ImageFilter.EDGE_ENHANCE)
Out[40]:

ImageFilter.EDGE_ENHANCE_MORE

In [41]:
img.filter(ImageFilter.EDGE_ENHANCE_MORE)
Out[41]:

ImageFilter.EMBOSS

In [42]:
img.filter(ImageFilter.EMBOSS)
Out[42]:

ImageFilter.FIND_EDGES

In [44]:
img.filter(ImageFilter.FIND_EDGES)
Out[44]:

ImageFilter.SHARPEN

In [45]:
img.filter(ImageFilter.SHARPEN)
Out[45]:

ImageFilter.SMOOTH

In [46]:
img.filter(ImageFilter.SMOOTH)
Out[46]:

ImageFilter.SMOOTH_MORE

In [47]:
img.filter(ImageFilter.SMOOTH_MORE)
Out[47]:

Comments