使用 Python 对 PNG 图像进行灰度化
通过服务器 API 创建对 PNG 图像和照片进行灰度化的 Python 应用
如何使用 Python 对 PNG 图像和照片进行灰度化
明亮的色彩通常被认为是精美照片的精髓。但如果无法选择彩色打印怎么办?在这种情况下,需要将彩色照片转换为灰度,令人惊讶的是,这并不一定会影响其影响。通常,从全彩到灰度的转换本身就是一种巧妙的技术,可以增强设计的魅力。该过程与消除颜色信息相结合,可以充当您的帮凶,突出图像细节,呈现表面纹理,并在光与影之间编排视觉交响曲,从而打造出独特的视觉效果。要将 PNG 图像转换为灰度,我们将利用 Aspose.Imaging for Python via .NET API 是一个功能丰富、功能强大且易于使用的图像处理和转换 API,适用于 Python 平台。您可以使用系统命令中的以下命令安装它。
系统命令行
>> pip install aspose-imaging-python-net
通过 Python 灰度 PNG 的步骤
您需要 aspose-imaging-python-net 在您自己的环境中尝试以下工作流程。
- 使用 Image.Load 方法加载 PNG 文件 +灰度图像;
- 以 Aspose.Imaging 支持的格式将压缩图像保存到光盘
系统要求
所有主要操作系统都支持 Python 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 带有 .NET Core 运行时的 Microsoft Windows / Linux。
- Python 和 PyPi 包管理器。
灰度 PNG 图像 - Python
from aspose.imaging import RasterImage, Image, IMultipageImage | |
from aspose.imaging.imageoptions import PngOptions | |
from aspose.pycore import as_of, is_assignable | |
import os | |
if 'TEMPLATE_DIR' in os.environ: | |
templates_folder = os.environ['TEMPLATE_DIR'] | |
else: | |
templates_folder = r"C:\Users\USER\Downloads\templates" | |
delete_output = 'SAVE_OUTPUT' not in os.environ | |
def delete_file(file): | |
if delete_output: | |
os.remove(file) | |
def grayscale_images(): | |
obj_init = [] | |
obj_init.append("jpg") | |
obj_init.append("png") | |
obj_init.append("bmp") | |
obj_init.append("apng") | |
obj_init.append("dicom") | |
obj_init.append("jp2") | |
obj_init.append("j2k") | |
obj_init.append("tga") | |
obj_init.append("webp") | |
obj_init.append("tiff") | |
obj_init.append("gif") | |
obj_init.append("ico") | |
raster_formats = obj_init | |
obj_init2 = [] | |
obj_init2.append("svg") | |
obj_init2.append("otg") | |
obj_init2.append("odg") | |
obj_init2.append("eps") | |
obj_init2.append("wmf") | |
obj_init2.append("emf") | |
obj_init2.append("wmz") | |
obj_init2.append("emz") | |
obj_init2.append("cmx") | |
obj_init2.append("cdr") | |
vector_formats = obj_init2 | |
all_formats = raster_formats | |
all_formats.extend(vector_formats) | |
for format_ext in all_formats: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
if not os.path.exists(input_file): | |
continue | |
is_vector_format = format_ext in vector_formats | |
if is_vector_format: | |
input_file = rasterize_vector_image(format_ext, input_file) | |
output_file = os.path.join(templates_folder, f"grayscale_{format_ext}.png") | |
print(f"Processing {format_ext}") | |
# explicit type casting from Image to RasterImage | |
with as_of(Image.load(input_file), RasterImage) as image: | |
# Additional code examples can be found at | |
# https://apireference.aspose.com/imaging/python-net/aspose.imaging/rasterimage/methods/grayscale | |
image.grayscale() | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
page_index = 0 | |
for page in multi_page.pages: | |
file_name = f"grayscale_page{page_index}_{format_ext}.png" | |
page.save(templates_folder + file_name, PngOptions()) | |
delete_file(templates_folder + file_name) | |
page_index += 1 | |
else: | |
image.save(output_file, PngOptions()) | |
delete_file(output_file) | |
if is_vector_format: | |
delete_file(input_file) | |
def rasterize_vector_image(format_ext, input_file): | |
output_file = os.path.join(templates_folder, f"rasterized.{format_ext}.png") | |
with Image.load(input_file) as image: | |
image.save(output_file, PngOptions()) | |
return output_file | |
grayscale_images() |
关于 Python API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。灰度 PNG 通过在线应用程序
访问我们的 Live Demos 网站 获取灰度 PNG 文档。 现场演示有以下好处
PNG 什么是 PNG 文件格式
PNG,便携式网络图形,是指一种使用无损压缩的光栅图像文件格式。此文件格式是作为图形交换格式 (GIF) 的替代品而创建的,没有版权限制。但是,PNG 文件格式不支持动画。 PNG文件格式支持无损图像压缩,使其在用户中很受欢迎。随着时间的推移,PNG 已经发展成为最常用的图像文件格式之一。几乎所有操作系统都支持打开 PNG 文件。例如,Microsoft Windows 查看器能够打开 PNG 文件,因为操作系统默认支持作为安装的一部分。
阅读更多其他支持的灰度格式
使用 Python,可以轻松地对不同格式进行灰度化,包括。