Python Pillow 简明教程
Python Pillow - Embossing Images
通常, embossing 用于在纸张或卡片纸上创建浮雕图像,以实现三维外观,英国邮政服务在 19 世纪就曾用它为邮票增添优雅气息。此过程包括提升图像、设计或文本的某些部分,以创建三维效果。
In general, embossing used for creating raised relief images on paper or cardstock to achieve a three-dimensional appearance, was notably used by the British postal service during the 19th century to add an elegant touch to stamps. This process involves raising certain parts of the image, design, or text to create a three-dimensional effect.
在图像处理和计算机图形学中, image embossing 是一种技术,其中图像中的每个像素都根据原始图像的明暗边界转换成加亮版本或阴影版本。对比度较低的位置将替换为灰色背景。生成的浮雕图像有效地表示了原始图像中每个位置的颜色变化率。对图像应用浮雕滤镜通常会生成输出,其类似于原始图像的纸张或金属浮雕呈现效果,因此得名。
In image processing and computer graphics, image embossing is a technique where each pixel in an image is transformed into either a highlighted or shadowed version, depending on the original image’s light and dark boundaries. Areas with low contrast are replaced by a gray background. The resulting embossed image effectively represents the rate of color change at each location within the original image. Applying an embossing filter to an image can often produce an output resembling a paper or metal-embossed rendition of the original image, hence its name.
Python 的 Pillow 库在 ImageFilter 模块中提供了一些标准图像滤镜,通过调用 image.filter() 方法对图像执行不同的滤镜操作。在本教程中,我们将看到 ImageFilter 模块提供的浮雕滤镜的工作原理。
Python’s Pillow library offers several standard image filters within the ImageFilter module to perform the different filter operations on the image by calling the image.filter() method. In this tutorial, we will see the working of the embossing filter provided by the ImageFilter module.
Applying ImageFilter.EMBOSS kernel filter with the Image.filter() method
ImageFilter.EMBOSS 滤镜是 Pillow 库当前版本中可用的内置滤镜选项之一,用于在图像中创建浮雕效果。
The ImageFilter.EMBOSS filter is one of the built-in filter options available in the current version of the Pillow library and is used to create an embossed effect in the image.
以下是如何应用图像浮雕的步骤 −
Following are the steps for applying image embossing −
-
Load the input image using the Image.open() function.
-
Apply the filter() function to the loaded Image object and provide ImageFilter.EMBOSS as an argument to the function. The function will return the embossed image as a PIL.Image.Image object.
Example
下面是一个使用 Image.filter() 方法与 ImageFilter.EMBOSS 内核滤镜的示例。
Here is an example using the Image.filter() method with ImageFilter.EMBOSS kernel filter.
from PIL import Image, ImageFilter
# Load an input image
input_image = Image.open("Images/TP logo.jpg")
# Apply an emboss effect to the image
embossed_result = input_image.filter(ImageFilter.EMBOSS)
# Display the input image
input_image.show()
# Display the modified image with the emboss effect
embossed_result.show()

输出经过浮雕滤镜处理的图像 −
Output filtered image with emboss effect −

Customizing the EMBOSS filter to add depth and azimuth to the embossed image
在 Pillow 库的源代码中,我们可以在“ImageFilter.py”模块中观察到 EMBOSS 类。此类表示浮雕滤镜,并提供了一种在图像上创建浮雕效果的基本方法。
In the source code of the pillow library, we can observe the EMBOSS class in the "ImageFilter.py," module. This class represents the embossing filter and provides a basic way to create an embossed effect on an image.
# Embossing filter.
class EMBOSS(BuiltinFilter):
name = "Emboss"
# fmt: off
filterargs = (3, 3), 1, 128, (
-1, 0, 0,
0, 1, 0,
0, 0, 0,
)
此滤镜通过逐像素对图像应用矩阵来工作。该矩阵包含决定应用于每个像素的转换值的变量。通过修改矩阵,你可以控制浮雕效果的方位角和强度。
The filter operates by applying a matrix to the image pixel by pixel. The matrix contains values that determine the transformation applied to each pixel. By modifying the matrix, you can control the azimuth and strength of the embossing effect.
该矩阵是一个 3x3 网格,其中每个元素对应于当前像素及其周围像素。中央值表示正在处理的当前像素。此滤镜根据这些值在矩阵中的权重将它们组合起来,以创建转换后的像素。你可以通过调整影响效果整体强度的 scale 和 offset 参数来定制此滤镜。
The matrix is a 3x3 grid where each element corresponds to the current pixel and its surrounding pixels. The central value represents the current pixel being processed. The filter combines these values based on their weights in the matrix to create the transformed pixel. You can customize the filter by adjusting the scale and offset parameters, which influence the overall strength of the effect.
Example
下面是一个示例,演示如何对图像应用自定义浮雕滤镜,它允许你通过调整各种参数来控制浮雕效果的外观。
Here is an example that demonstrates how to apply a custom emboss filter to an image, allowing you to control the appearance of the emboss effect by adjusting various parameters.
from PIL import Image, ImageFilter
# Open the input image
image = Image.open('Images/TP logo.jpg')
# modify the filterargs such as scale, offset and the matrix
ImageFilter.EMBOSS.filterargs=((3, 3), 2, 150, (0, 0, 0, 0, 1, 0, -2, 0, 0))
# Apply an emboss effect to the image
embossed_result = image.filter(ImageFilter.EMBOSS)
# Display the input image
image.show()
# Display the modified image with the emboss effect
embossed_result.show()

使用 Gemini 自定义浮雕效果输出过滤的图象 −
Output filtered image with custom emboss effect −
