Merge JPG to PNG
Contents
[
Hide
]
Merge JPG to PNG
Aspose.Imaging allows to merge images from JPG to PNG. Below example shows how to perform it for horizontal merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pycore as aspycore | |
from aspose.imaging import Image, Rectangle, RasterImage | |
from aspose.imaging.imageoptions import JpegOptions, PngOptions | |
from aspose.imaging.sources import FileCreateSource | |
from aspose.imaging.fileformats.jpeg import JpegImage | |
import os | |
import functools | |
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 | |
image_paths = [os.path.join(data_dir, "template.jpg"), os.path.join(data_dir, "template.jpeg")] | |
output_path = os.path.join(data_dir, "result.png") | |
temp_file_path = os.path.join(data_dir, "temp.jpg") | |
# Getting resulting image size. | |
image_sizes = [] | |
for image_path in image_paths: | |
with Image.load(image_path) as image: | |
image_sizes.append(image.size) | |
new_width = 0 | |
new_height = 0 | |
for size in image_sizes: | |
new_width += size.width | |
new_height = max(new_height, size.height) | |
# Combining images into new one. | |
temp_file_source = FileCreateSource(temp_file_path, delete_output) | |
with JpegOptions() as options: | |
options.source = temp_file_source | |
options.quality = 100 | |
with aspycore.as_of(Image.create(options, new_width, new_height), JpegImage) as new_image: | |
stitched_width = 0 | |
for image_path in image_paths: | |
with aspycore.as_of(Image.load(image_path), RasterImage) as image: | |
bounds = Rectangle(stitched_width, 0, image.width, image.height) | |
new_image.save_argb_32_pixels(bounds, image.load_argb_32_pixels(image.bounds)) | |
stitched_width += image.width | |
new_image.save(output_path, PngOptions()) | |
if delete_output: | |
os.remove(output_path) | |
if os.path.exists(temp_file_path): | |
os.remove(temp_file_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pycore as aspycore | |
from aspose.imaging import Image, Rectangle, RasterImage | |
from aspose.imaging.imageoptions import JpegOptions, PngOptions | |
from aspose.imaging.sources import StreamSource | |
from aspose.imaging.fileformats.jpeg import JpegImage | |
from aspose.imaging.extensions import StreamExtensions | |
import os | |
import functools | |
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 | |
image_paths = [os.path.join(data_dir, "template.jpg"), os.path.join(data_dir, "template.jpeg")] | |
output_path = os.path.join(data_dir, "result.png") | |
temp_file_path = os.path.join(data_dir, "temp.jpg") | |
# Getting resulting image size. | |
image_sizes = [] | |
for image_path in image_paths: | |
with Image.load(image_path) as image: | |
image_sizes.append(image.size) | |
new_width = 0 | |
new_height = 0 | |
for size in image_sizes: | |
new_height += size.height | |
new_width = max(new_width, size.width) | |
# Combining images into new one. | |
with StreamExtensions.create_memory_stream() as memory_stream: | |
output_stream_source = StreamSource(memory_stream) | |
with JpegOptions() as options: | |
options.source = output_stream_source | |
options.quality = 100 | |
with aspycore.as_of(Image.create(options, new_width, new_height), JpegImage) as new_image: | |
stitched_height = 0 | |
for image_path in image_paths: | |
with aspycore.as_of(Image.load(image_path), RasterImage) as image: | |
bounds = Rectangle(0, stitched_height, image.width, image.height) | |
new_image.save_argb_32_pixels(bounds, image.load_argb_32_pixels(image.bounds)) | |
stitched_height += image.height | |
new_image.save(output_path, PngOptions()) | |
if delete_output: | |
os.remove(output_path) |