使用 Python 进行 JP2 图像合并
创建 Python 应用程序以通过服务器 API 合并 JP2 图像和照片
如何使用 Python 合并 JP2 图像和照片
利用拼贴技术,您可以从一组预先存在的图像和照片中制作出迷人的作品。 Python 库提供了无缝合并图像和照片的功能,无论其源文件格式如何变化。突出您的作品集的有效策略是使用带有重复图像或图案的壁纸装饰,允许水平和垂直排列。如果您的目标是展示文件处理的结果,那么在应用图像效果之前和之后无缝合并两个图像是一项简单的任务。要合并 JP2 图像,我们将使用 Aspose.Imaging for Python via .NET API 是一个功能丰富、功能强大且易于使用的图像处理和转换 API,适用于 Python 平台。您可以使用系统命令中的以下命令安装它。
系统命令行
>> pip install aspose-imaging-python-net
通过 Python 合并 JP2 的步骤
您需要 aspose-imaging-python-net 在您自己的环境中尝试以下工作流程。
- 使用 Image.Load 方法加载 JP2 文件
- 将图像组合成新图像
- 以 Aspose.Imaging 支持的格式将合并后的图像保存到光盘
系统要求
所有主要操作系统都支持 Python 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 带有 .NET Core 运行时的 Microsoft Windows / Linux。
- Python 和 PyPi 包管理器。
合并 JP2 图像 - Python
from aspose.imaging import Image, Graphics, Color, Rectangle | |
from aspose.imaging.fileformats.png import PngColorType | |
from aspose.imaging.imageoptions import PngOptions | |
from aspose.imaging.sources import StreamSource | |
import os | |
import tempfile | |
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 | |
data_dir = templates_folder | |
images = [] | |
files = ["template.png", "template.jpg"] | |
merge_direction = [0, 1] | |
max_width = 0 | |
max_height = 0 | |
total_width = 0 | |
total_height = 0 | |
for file_name in files: | |
image = Image.load(os.path.join(data_dir, file_name)) | |
total_width += image.width | |
if image.width > max_width: | |
max_width = image.width | |
total_height += image.height | |
if image.height > max_height: | |
max_height = image.height | |
images.append(image) | |
def get_temp_file_name(): | |
f = tempfile.NamedTemporaryFile() | |
file_name = f.name | |
f.close() | |
return file_name | |
def merge_images(direction): | |
target_width = 0 | |
target_height = 0 | |
if direction == 0: | |
target_width = total_width | |
target_height = max_height | |
else: | |
target_width = max_width | |
target_height = total_height | |
output_path = data_dir | |
output_path = os.path.join(output_path, "result" + str(direction) + ".png") | |
png_options = PngOptions() | |
png_options.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
tmp_file = get_temp_file_name() | |
with open(tmp_file, "wb") as stream: | |
png_options.source = StreamSource(stream) | |
with Image.create(png_options, target_width, target_height) as image: | |
image.background_color = Color.white | |
graphics = Graphics(image) | |
x = 0 | |
y = 0 | |
graphics.begin_update() | |
for frame in images: | |
print("x", x, "y", y) | |
graphics.draw_image(frame, Rectangle(x, y, frame.width, frame.height)) | |
if direction == 0: | |
x += frame.width | |
if direction == 1: | |
y += frame.height | |
graphics.end_update() | |
image.save(output_path) | |
os.remove(tmp_file) | |
if delete_output: | |
os.remove(output_path) | |
# run | |
merge_images(0) | |
merge_images(1) | |
for image in images: | |
# to dispose the image we call __exit__() | |
with image as _: | |
pass |
关于 Python API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。通过在线应用合并 JP2
通过访问我们的 Live Demos 网站 合并 JP2 文档。 现场演示有以下好处
JP2 什么是 JP2 文件格式
JPEG 2000 (JP2) 是一种图像编码系统和最先进的图像压缩标准。设计,使用小波技术 JPEG 2000 可以一次编码任何质量的无损内容。此外,在编码效率没有任何实质性损失的情况下,JPEG 2000 能够有效地访问相同的内容并将其解码为各种其他分辨率和质量。 JPEG 2000 中的码流具有显着的可扩展性,具有为空间随机访问提供便利的感兴趣区域。拥有多达 16384 个不同的组件,尺寸以万亿像素为单位,精度可高达 38 位/样本。
阅读更多其他支持的合并格式
使用 Python,可以轻松合并不同的格式,包括。