Python PIL |内核()方法
PIL 是Python Imaging Library,它为Python解释器提供了图像编辑功能。 ImageFilter模块包含一组预定义过滤器的定义,可与Image.filter()
方法一起使用。
PIL.ImageFilter.Kernel()
创建卷积核。当前版本仅支持 3×3 和 5×5 整数和浮点内核。
Syntax: PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0)
Parameters:
size – Kernel size, given as (width, height). In the current version, this must be (3, 3) or (5, 5).
kernel – A sequence containing kernel weights.
scale – Scale factor. If given, the result for each pixel is divided by this value. the default is the sum of the kernel weights.
offset – Offset. If given, this value is added to the result, after it has been divided by the scale factor.
Returns type: An image.
使用的图像:
# Importing Image and ImageFilter module from PIL package
from PIL import Image, ImageFilter
# creating a image object
im1 = Image.open(r"C:\Users\System-Pc\Desktop\leave.JPG")
# applying the Kernel filter
im2 = im1.filter(ImageFilter.Kernel((3, 3),
(-1, -1, -1, -1, 9, -1, -1, -1, -1), 1, 0))
im2 = im2.show()
输出:
另一个例子:这里改变内核值来获得输出,我们也可以改变其他参数。
# Importing Image and ImageFilter module from PIL package
from PIL import Image, ImageFilter
# Importing Image and ImageFilter module from PIL package
from PIL import Image, ImageFilter
# creating a image object
im1 = Image.open(r"C:\Users\System-Pc\Desktop\leave.JPG")
# applying the Kernel filter
im2 = im1.filter(ImageFilter.Kernel((3, 3),
(-1, -1, -1, -1, 11, -2, -2, -2, -2), 1, 0))
im2 = im2.show()
输出: