Python Pillow 为图像添加过滤器 | 您所在的位置:网站首页 › vf中find和seek的区别 › Python Pillow 为图像添加过滤器 |
Python Pillow 为图像添加过滤器 ImageFilter模块 包含一组预定义的过滤器的定义,我们用 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的模糊图像,如下所示 原始图像 模 糊的图像 用迷你过滤器模糊的图像 滤波器当前版本的枕头库提供了下面提到的一组预定义的图像增强过滤器。 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显示工具的过滤后的图像,如下所示 原始图像 过滤后 的图像 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显示工具的过滤后的图像,如下所示 原始图像 过滤后 的图像 |
CopyRight 2018-2019 实验室设备网 版权所有 |