ImageFilter Module

Note

All source images in this document are derived from https://www.pexels.com (CC0 License).

Filters

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#filters, http://effbot.org/imagingbook/imagefilter.htm#filters
srcimg05.jpg srcimg05
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (
    BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
    EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN
    )
img = Image.open('data/srcimg05.jpg')
for f in (
    BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE,
    EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN):

    img.filter(f).save(
        "result/ImageFilter_predefs_{}_01.jpg".format(
            f.name.replace(" ", "-")))
Blur ImageFilter.predefs.res01 Contour ImageFilter.predefs.res02 Detail ImageFilter.predefs.res03 Edge-enhance ImageFilter.predefs.res04 Edge-enhance More ImageFilter.predefs.res05
Emboss ImageFilter.predefs.res06 Find Edges ImageFilter.predefs.res07 Smooth ImageFilter.predefs.res08 Smooth More ImageFilter.predefs.res09 Sharpen ImageFilter.predefs.res10

class GaussianBlur

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.GaussianBlur
srcimg05.jpg srcimg05
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (
    GaussianBlur
    )
simg = Image.open('data/srcimg05.jpg')

# defaut: radius=2
dimg = simg.filter(GaussianBlur(radius=10))
dimg.save("result/ImageFilter_GaussianBlur_10.jpg")
ImageFilter.GaussianBlur.res01

class UnsharpMask

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.UnsharpMask
srcimg07.jpg srcimg07
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (
    UnsharpMask
    )
simg = Image.open('data/srcimg07.jpg')

# defaut: radius=2, percent=150, threshold=3
dimg = simg.filter(UnsharpMask(radius=2, percent=150, threshold=3))
dimg.save("result/ImageFilter_UnsharpMask_2_150_3.jpg")
ImageFilter.UnsharpMask.res01

class Kernel

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.Kernel, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.Kernel
srcimg05.jpg srcimg05
from PIL import Image
from PIL import ImageFilter
img = Image.open('data/srcimg05.jpg')

# one kind of emboss
km = (
     -2, -1,  0,
     -1,  1,  1,
      0,  1,  2
      )
k = ImageFilter.Kernel(
    size=(3, 3),
    kernel=km,
    scale=sum(km),  # default
    offset=0  # default
    )
img.filter(k).save(
    "result/ImageFilter_Kernel_01.jpg")
ImageFilter.Kernel.res01
from PIL import Image
from PIL import ImageFilter
import numpy as np

img = Image.open('data/srcimg05.jpg')

# Gaussian blur 5 x 5 (approximation)
km = np.array((
        (1, 4, 6, 4, 1),
        (4, 16, 24, 16, 4),
        (6, 24, 36, 24, 6),
        (4, 16, 24, 16, 4),
        (1, 4, 6, 4, 1),
      )) / 256.
k = ImageFilter.Kernel(
    size=km.shape,
    kernel=km.flatten(),
    scale=np.sum(km),  # default
    offset=0  # default
    )
img.filter(k).save(
    "result/ImageFilter_Kernel_02.jpg")
ImageFilter.Kernel.res02

class RankFilter, MedianFilter, MinFilter, MaxFilter

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.RankFilter, http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.MedianFilter, http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.MinFilter, http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.MaxFilter http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.RankFilter, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.MedianFilter, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.MinFilter, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.MaxFilter

Note

MedianFilter, MinFilter, and MaxFilter are subclasses of RankFilter.

srcimg06.jpg srcimg06
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (
    RankFilter, MedianFilter, MinFilter, MaxFilter
    )
simg = Image.open('data/srcimg06.jpg')

#
dimg = simg.filter(RankFilter(size=9, rank=2))
dimg.save("result/ImageFilter_RankFilter_9_2.jpg")
#
dimg = simg.filter(MinFilter(size=9))
dimg.save("result/ImageFilter_MinFilter_9.jpg")
#
dimg = simg.filter(MedianFilter(size=9))
dimg.save("result/ImageFilter_MedianFilter_9.jpg")
#
dimg = simg.filter(MaxFilter(size=9))
dimg.save("result/ImageFilter_MaxFilter_9.jpg")
RankFilter(size=9, rank=2) ImageFilter.RankFilter.res01 MinFilter(size=9) ImageFilter.RankFilter.res03 MedianFilter(size=9) ImageFilter.RankFilter.res02 MaxFilter(size=9) ImageFilter.RankFilter.res04

class ModeFilter

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageFilter.html#PIL.ImageFilter.ModeFilter, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.ModeFilter
srcimg06.jpg srcimg06
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (
    ModeFilter
    )
simg = Image.open('data/srcimg06.jpg')

#
dimg = simg.filter(ModeFilter(size=9))
dimg.save("result/ImageFilter_ModeFilter_9.jpg")
ImageFilter.ModeFilter.res01