Python Pillow 向图像添加过滤器
The 图像过滤模块 包含一组预定义过滤器的定义,我们将其用于 图像过滤器() 方法。这些滤镜用于改变图像的外观和感觉。
例子
下面的例子是过滤图像:
from PIL import Image, ImageFilter im = Image.open('jungleSaf2.jpg') im1 = im.filter(ImageFilter.BLUR) im1.show() im2 = im.filter(ImageFilter.MinFilter(3)) im2.show() im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3) im3.show()
在上面的程序中,我们使用了 最小过滤器() 方法,用于创建最小过滤器。它选择具有给定大小的窗口中的最低像素值。
ImageFilter.MinFilter(size=3)
Where,
size : 内核大小,以像素为单位。
如果保存上述程序并执行,它会显示原始图像、模糊图像和使用标准 PNG 显示实用程序使用 MinFilter 的模糊图像,如下所示:
原始图像
图像模糊
使用迷你滤镜模糊的图像
Filters
当前版本的Pillow库提供了下面提到的一组预定义的图像增强过滤器。
-
BLUR
-
CONTOUR
-
DETAIL
-
EDGE_ENHANCE
-
EDGE_ENHANCE_MORE
-
EMBOSS
-
寻找边缘
-
SHARPEN
-
SMOOTH
-
SMOOTH_MORE
例子
以下 python 示例对图像应用模糊过滤器将其保存并使用标准 PNG 显示实用程序显示:
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(BLUR) img1.save('images/ImageFilter_blur.jpg') img1.show()
以同样的方式,对 image.filter() 方法你可以传递以下任何参数来获取相应的输出:
-
CONTOUR
-
DETAIL
-
EDGE_ENHANCE
-
EDGE_ENHANCE_MORE
-
EMBOSS
-
寻找边缘
-
SMOOTH
-
SMOOTH_MORE
-
SHARPEN
Python img.filter(CONTOUR) 方法
以下 python 示例将 CONTOUR 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(CONTOUR) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像和使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(DETAIL) 方法
以下 python 示例将 DETAIL 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(DETAIL) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像,以及使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(EDGE_ENHANCE) 方法
以下 python 示例将 EDGE_ENHANCE 过滤器应用于给定图像:
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(EDGE_ENHANCE) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像,以及使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(EDGE_ENHANCE_MORE) 方法
以下 python 示例将 EDGE_ENHANCE_MORE 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(EDGE_ENHANCE_MORE) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像,以及使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(EMBOSS) 方法
以下 python 示例将 EMBOSS 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(EMBOSS) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像和使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(FIND_EDGES) 方法
以下 python 示例将 FIND_EDGES 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(FIND_EDGES) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像和使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(SMOOTH) 方法
以下 python 示例将 SMOOTH 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(SMOOTH) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像和使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
Python img.filter(SHARPEN) 方法
以下 python 示例将 SHARPEN 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(SHARPEN) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像和使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像
以下 python 示例将 SHARPEN 过滤器应用于给定图像。
例子
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open('images/cat.jpg') #Applying the blur filter img1 = img.filter(SHARPEN) img1.save('images/ImageFilter_blur.jpg') img1.show()
如果保存上述程序并执行,它会显示原始图像和使用标准 PNG 显示实用程序过滤后的图像,如下所示:
原始图像
过滤后的图像