使用 Python 进行 ODG 图像压缩
创建 Python 应用程序以通过服务器 API 压缩 ODG 图像和照片
如何使用 Python 压缩 ODG 图像和照片
图像的出版改编不仅包括创作过程,还包括技术调整,例如文件压缩。一般来说,印刷材料或户外广告需要高分辨率图像,而网站可能面临大文件大小的挑战。图像压缩设置可能会根据其预期用途和发布地点而有所不同。下载大文件可能需要相当长的时间,尤其是在移动连接上,从而影响整体用户体验。然而,过度压缩的图像可能会出现模糊和明显的像素化,从而损害视觉质量。要在文件大小和图像质量之间实现平衡,需要仔细选择算法和压缩级别。要以 ODG 格式压缩图像,我们将采用 Aspose.Imaging for Python via .NET API 是一个功能丰富、功能强大且易于使用的图像处理和转换 API,适用于 Python 平台。您可以使用系统命令中的以下命令安装它。
系统命令行
>> pip install aspose-imaging-python-net
通过 Python 压缩 ODG 的步骤
您需要 aspose-imaging-python-net 在您自己的环境中尝试以下工作流程。
- 使用 Image.Load 方法加载 ODG 文件 +压缩图像;
- 以 Aspose.Imaging 支持的格式将压缩图像保存到光盘
系统要求
所有主要操作系统都支持 Python 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 带有 .NET Core 运行时的 Microsoft Windows / Linux。
- Python 和 PyPi 包管理器。
压缩 ODG 图像 - Python
from aspose.imaging.imageoptions import * | |
from aspose.imaging import * | |
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 compress_vector_format_to_svg(): | |
obj_init = [] | |
obj_init.append("cdr") | |
obj_init.append("cmx") | |
obj_init.append("odg") | |
obj_init.append("otg") | |
obj_init.append("eps") | |
format_exts = obj_init | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init2 = CdrRasterizationOptions() | |
obj_init2.page_width = float(image.width) | |
obj_init2.page_height = float(image.height) | |
return obj_init2 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init3 = CmxRasterizationOptions() | |
obj_init3.page_width = float(image.width) | |
obj_init3.page_height = float(image.height) | |
return obj_init3 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init4 = OdgRasterizationOptions() | |
obj_init4.page_width = float(image.width) | |
obj_init4.page_height = float(image.height) | |
return obj_init4 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init5 = OtgRasterizationOptions() | |
obj_init5.page_width = float(image.width) | |
obj_init5.page_height = float(image.height) | |
return obj_init5 | |
else: | |
return None | |
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: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init6 = SvgOptions() | |
obj_init6.compress = True | |
obj_init6.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.svgz" | |
page.save(out_file, obj_init6) | |
if delete_output: | |
os.remove(out_file) | |
page_index += 1 | |
else: | |
obj_init7 = SvgOptions() | |
obj_init7.compress = True | |
obj_init7.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".svgz", obj_init7) | |
if delete_output: | |
os.remove(output_file + ".svgz") | |
def compress_vector_format_to_wmf(): | |
obj_init8 = [] | |
obj_init8.append("cdr") | |
obj_init8.append("cmx") | |
obj_init8.append("odg") | |
obj_init8.append("otg") | |
obj_init8.append("eps") | |
format_exts = obj_init8 | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init9 = CdrRasterizationOptions() | |
obj_init9.page_width = float(image.width) | |
obj_init9.page_height = float(image.height) | |
return obj_init9 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init10 = CmxRasterizationOptions() | |
obj_init10.page_width = float(image.width) | |
obj_init10.page_height = float(image.height) | |
return obj_init10 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init11 = OdgRasterizationOptions() | |
obj_init11.page_width = float(image.width) | |
obj_init11.page_height = float(image.height) | |
return obj_init11 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init12 = OtgRasterizationOptions() | |
obj_init12.page_width = float(image.width) | |
obj_init12.page_height = float(image.height) | |
return obj_init12 | |
else: | |
return None | |
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: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init13 = WmfOptions() | |
obj_init13.compress = True | |
obj_init13.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.wmz" | |
page.save(out_file, obj_init13) | |
if delete_output: | |
os.remove(out_file) | |
else: | |
obj_init14 = WmfOptions() | |
obj_init14.compress = True | |
obj_init14.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".wmz", obj_init14) | |
if delete_output: | |
os.remove(output_file + ".wmz") | |
def compress_vector_formats_to_emf(): | |
obj_init15 = [] | |
obj_init15.append("cdr") | |
obj_init15.append("cmx") | |
obj_init15.append("odg") | |
obj_init15.append("otg") | |
obj_init15.append("eps") | |
format_exts = obj_init15 | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init16 = CdrRasterizationOptions() | |
obj_init16.page_width = float(image.width) | |
obj_init16.page_height = float(image.height) | |
return obj_init16 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init17 = CmxRasterizationOptions() | |
obj_init17.page_width = float(image.width) | |
obj_init17.page_height = float(image.height) | |
return obj_init17 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init18 = OdgRasterizationOptions() | |
obj_init18.page_width = float(image.width) | |
obj_init18.page_height = float(image.height) | |
return obj_init18 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init19 = OtgRasterizationOptions() | |
obj_init19.page_width = float(image.width) | |
obj_init19.page_height = float(image.height) | |
return obj_init19 | |
else: | |
return None | |
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: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init20 = EmfOptions() | |
obj_init20.compress = True | |
obj_init20.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.emz" | |
page.save(out_file, obj_init20) | |
if delete_output: | |
os.remove(out_file) | |
else: | |
obj_init21 = EmfOptions() | |
obj_init21.compress = True | |
obj_init21.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".emz", obj_init21) | |
if delete_output: | |
os.remove(output_file + ".emz") | |
# run | |
compress_vector_formats_to_emf() | |
compress_vector_format_to_svg() | |
compress_vector_format_to_wmf() |
关于 Python API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。通过在线应用压缩 ODG
通过访问我们的 Live Demos 网站 压缩 ODG 文档。 现场演示有以下好处
ODG 什么是 ODG 文件格式
Apache OpenOffice 的 Draw 应用程序使用 ODG 文件格式将绘图元素存储为矢量图像。它遵循结构信息标准推进 (OASIS) 概述的基于 XML 的文件格式规范。 ODG 将绘图表示为使用点、线和曲线的矢量图像。除了 OpenOffice,LibreOffice 和其他应用程序还提供对 ODG 文件格式的支持。例如,OpenOffice 支持的其他格式包括 ODT、ODF、ODP 和 ODS。
阅读更多其他支持的压缩格式
使用 Python,可以轻松压缩不同的格式,包括。