本指南详细介绍了如何使用 Python 模糊 image。它解释了系统设置、算法以及使用 Python 模糊图片的运行代码片段。您可以修改此代码以自定义图像模糊效果。此外,您还可以以不同的图像格式(如 JPG、PNG、GIF、BMP 等)渲染输出图像。
使用 Python 模糊图片的步骤
- 通过安装 Aspose.Imaging for Python 配置系统来模糊照片
- 创建 Image 类的实例以获取输入图像
- 将图像转换为光栅图像
- 初始化 GaussianBlurFilterOptions 类的对象
- 使用 save 方法写入输出模糊照片
这些步骤概述了如何使用 Python 为照片添加模糊效果。要启动该过程,请读取输入图像文件并将其转换为光栅图像。随后,定义所需的选项并根据您的偏好渲染输出图像。
使用 Python 实现照片模糊的代码
import aspose.imaging | |
import aspose.pycore as aspycore | |
from aspose.imaging import Image, RasterImage | |
from aspose.imaging.imagefilters.filteroptions import GaussianBlurFilterOptions | |
import os | |
path = "C://SampleFiles//" | |
license = aspose.imaging.License() | |
license.set_license(path + "License.lic") | |
# Load the input image | |
with Image.load(os.path.join(path, "grayscaled.jpg")) as image: | |
# Cast the image into RasterImage | |
if aspycore.is_assignable(image, RasterImage): | |
raster_image = aspycore.as_of(image, RasterImage) | |
# Create the GaussianBlurFilterOptions object | |
options = GaussianBlurFilterOptions(5,5) | |
raster_image.filter(image.bounds, options) | |
image.save(os.path.join(path, "blur.png")) | |
print("Image Blurred Successfully") |
此示例代码演示了如何在 Python 中将模糊效果应用于图片。它主要与 Image 类配合使用,从流或磁盘读取输入图片。然后通过指定半径和 sigma 值应用高斯模糊,以自定义输出模糊效果。
在本教程中,我们探索了使用 Python 模糊照片。但是,如果您需要翻转图像,请前往 在 Python 中翻转图像 上的文章。