ImageFilter Module¶
Note
All source images in this document are derived from https://www.pexels.com (CC0 License).
Filters¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageFilter.html#filters, http://effbot.org/imagingbook/imagefilter.htm#filters
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 |
Contour |
Detail |
Edge-enhance |
Edge-enhance More |
Emboss |
Find Edges |
Smooth |
Smooth More |
Sharpen |
class GaussianBlur¶
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")
|
class UnsharpMask¶
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")
|
class Kernel¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageFilter.html#PIL.ImageFilter.Kernel, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.Kernel
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")
|
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")
|
class RankFilter, MedianFilter, MinFilter, MaxFilter¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageFilter.html#PIL.ImageFilter.RankFilter, https://pillow.readthedocs.io/en/latest/reference/ImageFilter.html#PIL.ImageFilter.MedianFilter, https://pillow.readthedocs.io/en/latest/reference/ImageFilter.html#PIL.ImageFilter.MinFilter, https://pillow.readthedocs.io/en/latest/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.
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) |
MinFilter(size=9) |
MedianFilter(size=9) |
MaxFilter(size=9) |
class ModeFilter¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageFilter.html#PIL.ImageFilter.ModeFilter, http://effbot.org/imagingbook/imagefilter.htm#tag-ImageFilter.ModeFilter
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")
|