📜  Python Pillow – 模糊图像

📅  最后修改于: 2022-05-13 01:54:40.966000             🧑  作者: Mango

Python Pillow – 模糊图像

对图像进行模糊处理是降低图像中噪声水平的过程,是图像处理的重要方面之一。在本文中,我们将学习使用枕头库模糊图像。为了模糊图像,我们在图像对象上使用了该库的 ImageFilter 类的一些方法。

注意:用于在所有不同方法中模糊的图像如下:

ImageFilter 类提供的方法:

1. PIL.ImageFilter.BoxBlur() 通过将每个像素设置为在每个方向上扩展半径像素的方框中像素的平均值来模糊图像。支持任意大小的浮动半径。使用优化的实现,该实现相对于任何半径值的图像大小以线性时间运行。

Python3
# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
 
# Blurring the image
im1 = im.filter(ImageFilter.BoxBlur(4))
 
# Shows the image in image viewer
im1.show()


Python3
# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
 
# Blurring the image
im1 = im.filter(ImageFilter.GaussianBlur(4))
 
# Shows the image in image viewer
im1.show()


Python3
# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
 
# Blurring the image
im1 = im.filter(ImageFilter.BLUR)
 
# Shows the image in image viewer
im1.show()


输出 :

蟒蛇枕盒模糊

2. PIL.ImageFilter.GaussianBlur() 此方法创建一个高斯模糊过滤器。过滤器使用半径作为参数,通过改变这个半径的值,图像上的模糊强度就会改变。函数的参数半径负责模糊强度。通过改变半径的值,GuassianBlur 的强度也随之改变。

蟒蛇3

# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
 
# Blurring the image
im1 = im.filter(ImageFilter.GaussianBlur(4))
 
# Shows the image in image viewer
im1.show()

输出 :

高斯模糊蟒蛇枕头

3. 简单模糊:它通过特定内核或卷积矩阵对指定的图像应用模糊效果。它不需要任何参数。

蟒蛇3

# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
 
# Blurring the image
im1 = im.filter(ImageFilter.BLUR)
 
# Shows the image in image viewer
im1.show()

输出 :