Use Python for J2K Images Merge
Create Python Apps to Merge J2K Images and Photos via Server APIs
How to Merge J2K Images and Photos with Python
Utilizing the collage technique empowers you to craft a captivating composition from a collection of pre-existing images and photos. The Python library provides the functionality to merge images and photos seamlessly, regardless of variations in their source file formats. An effective strategy to highlight your portfolio involves employing wallpapers ornament with recurring images or patterns, allowing for both horizontal and vertical arrangements. Should your objective be showcasing the outcome of file processing, seamlessly merging two images — before and after applying the image effect — is a straightforward task. To merge J2K images, we will employ Aspose.Imaging for Python via .NET API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Python platform. You may install it using the following command from your system command.
The system command line
>> pip install aspose-imaging-python-net
Steps to Merge J2K via Python
You need the aspose-imaging-python-net to try the following workflow in your own environment.
- load J2K files with Image.Load method;
- combining images into new one;
- save merged image to disc in the supported by Aspose.Imaging format.
System Requirements
Aspose.Imaging for Python is supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows / Linux with .NET Core Runtime.
- Python and PyPi package manager.
Merge J2K images - 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 |
About Aspose.Imaging for Python API
Aspose.Imaging API is an image processing solution to create, modify, draw or convert images (photos) within applications. It offers: cross-platform Image processing, including but not limited to conversions between various image formats (including uniform multi-page or multi-frame image processing), modifications such as drawing, working with graphic primitives, transformations (resize, crop, flip&rotate, binarization, grayscale, adjust), advanced image manipulation features (filtering, dithering, masking, deskewing), and memory optimization strategies. It’s a standalone library and does not depend on any software for image operations. One can easily add high-performance image conversion features with native APIs within projects. These are 100% private on-premise APIs and images are processed at your servers.Merge J2K via Online App
Merge J2K documents by visiting our Live Demos website The live demo has the following benefits
J2K What is J2K File Format
A J2K file is an image that is compressed using the wavelet compression instead of DCT compression.
Read MoreOther Supported Merge Formats
Using Python, one can easily merge different formats including: