Pillow - 向图像添加过滤器
-
简述
这ImageFilter module包含一组预定义过滤器的定义,我们将其用于Image.filter()方法。这些滤镜用于改变图像的外观和感觉。例子
下面的示例是过滤图像 -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()
在上面的程序中,我们使用了MinFilter()方法,用于创建最小过滤器。它选择具有给定大小的窗口中的最低像素值。ImageFilter.MinFilter(size=3)
在哪里,size− 内核大小,以像素为单位。输出
如果保存上述程序并执行,它会使用标准 PNG 显示实用程序显示原始图像、模糊图像和带有 MinFilter 的模糊图像,如下所示 -Original ImageBlurred ImageImage blurred with mini filter -
过滤器
当前版本的枕头库提供了下面提到的一组预定义的图像增强过滤器。-
BLUR
-
CONTOUR
-
DETAIL
-
EDGE_ENHANCE
-
EDGE_ENHANCE_MORE
-
EMBOSS
-
FIND_EDGES
-
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
-
FIND_EDGES
-
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image -
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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image以下 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 显示实用程序过滤后的图像,如下所示 -Original imageFiltered image