Working with multipage image formats
Aspose.Imaging supports rich functionality to work with multipage image formats such as Gif, Tiff, Psd, Dicom, Cdr, WebP etc. Also, using Aspose.Imaging image can be exported also to multipage PDF document.
To indicate whether the image contains layers/pages/ frames the IMultipageImage interface has been introduced. All multi-page images such as PSD, CDR, GIF, etc. are descendants of this interface. Using Pages property, you can access the pages of any multi-page image in the library.
Example of usage IMultipageImage interface:
import aspose.pycore as aspycore | |
from aspose.imaging import Image, IMultipageImage | |
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 | |
data_dir = templates_folder | |
with Image.load(os.path.join(data_dir, "template.tiff")) as image: | |
if aspycore.is_assignable(image, IMultipageImage): | |
pages = (aspycore.as_of(image, IMultipageImage)).pages | |
print("pages:", pages) | |
else: | |
print("Not a IMultipageImage!") |
Export of multipage images can be performed using MultiPageOptions class. With this option you can specify the pages that you want to export to another format. In the case of export to a single-page format, the 1st page of the range will be exported; in the case of export to a multi-page format, all pages of the range will be exported.
Example of export from multi-page format to single-page image format:
from aspose.imaging import IntRange, Image | |
from aspose.imaging.imageoptions import PngOptions, MultiPageOptions | |
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 | |
data_dir = templates_folder | |
start_page = 0 | |
count_page = 1 | |
with Image.load(os.path.join(data_dir, "template.tiff")) as image: | |
png_options = PngOptions() | |
png_options.multi_page_options = MultiPageOptions(IntRange(start_page, count_page)) | |
image.save(os.path.join(data_dir, "result.png"), png_options) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.png")) |
Example of export from multi-page format to multi-page
import aspose.pycore as aspycore | |
from aspose.imaging import IntRange, Image | |
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.imaging.imageoptions import TiffOptions, MultiPageOptions | |
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 | |
data_dir = templates_folder | |
start_page = 0 | |
count_page = 1 | |
with Image.load(os.path.join(data_dir, "template.tiff")) as image: | |
tiff_options = TiffOptions(TiffExpectedFormat.TIFF_DEFLATE_RGB) | |
tiff_options.multi_page_options = MultiPageOptions(IntRange(start_page, count_page)) | |
image.save(os.path.join(data_dir, "result.tiff"), tiff_options) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.tiff")) |
Here the 4th and 5th pages will be exported to the tiff format
Below presented example of export from/to different multipage image formats:
import aspose.pycore as aspycore | |
from aspose.imaging import * | |
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.imaging.imageoptions import * | |
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 | |
data_dir = templates_folder | |
# Code for using ExportImage method | |
def export_image(image_options, ext): | |
with Image.load(os.path.join(data_dir, "template.tiff")) as image: | |
# export only 1 pages | |
if aspycore.is_assignable(image, IMultipageImage) \ | |
and (aspycore.as_of(image, IMultipageImage)).pages is not None \ | |
and (aspycore.as_of(image, IMultipageImage)).page_count > 2: | |
image_options.multi_page_options = MultiPageOptions(IntRange(0, 1)) | |
else: | |
image_options.multi_page_options = None | |
if aspycore.is_assignable(image, VectorImage): | |
image_options.vector_rasterization_options = aspycore.as_of(image.get_default_options([Color.white, image.width, image.height]), VectorRasterizationOptions) | |
image_options.vector_rasterization_options.text_rendering_hint = TextRenderingHint.SINGLE_BIT_PER_PIXEL | |
image_options.vector_rasterization_options.smoothing_mode = SmoothingMode.NONE | |
out_file_name = os.path.join(data_dir, "result" + ext) | |
image.save(out_file_name, image_options) | |
if delete_output: | |
os.remove(out_file_name) | |
# Run | |
image_options = [WebPOptions(), GifOptions(), TiffOptions(TiffExpectedFormat.DEFAULT), BmpOptions(), JpegOptions(), Jpeg2000Options(), PngOptions(), EmfOptions(), SvgOptions(), WmfOptions(), PdfOptions()] | |
image_ext = [".webp", ".gif", ".tiff", ".bmp", ".jpeg", ".j2k", ".png", ".emf", ".svg", ".wmf", ".pdf"] | |
if len(image_options) != len(image_ext): | |
raise Exception("image_options length is not equal image_ext length") | |
for i in range(len(image_options)): | |
export_image(image_options[i], image_ext[i]) |
Support of making gifs and other multi-page (multi-frame) files from set of images
Create multipage images using AddPage method
You can create multipage image using add_page() method. The following code shows how you can create animated images using image frames from the folder:
import aspose.pycore as aspycore | |
from aspose.imaging import RasterImage, Image | |
from aspose.imaging.fileformats.apng import ApngImage | |
from aspose.imaging.fileformats.dicom import DicomImage | |
from aspose.imaging.fileformats.webp import WebPImage | |
from aspose.imaging.fileformats.tiff import TiffImage, TiffFrame | |
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.imaging.imageoptions import TiffOptions, ApngOptions, DicomOptions | |
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 | |
data_dir = templates_folder | |
def load_frames(): | |
out_list = list() | |
for file_path in ["template.png", "template.tiff", "template.bmp"]: | |
out_list.append(aspycore.as_of(Image.load(os.path.join(data_dir, file_path)), RasterImage)) | |
return out_list | |
# Load frames | |
frames = load_frames() | |
# Create TIFF image using the first frame | |
with TiffImage(TiffFrame(frames[0])) as image: | |
# Add frames to the TIFF image using the AddPage method | |
for index in range(1, len(frames)): | |
image.add_page(frames[index]) | |
# Save TIFF image using options | |
options = TiffOptions(TiffExpectedFormat.TIFF_JPEG_RGB) | |
image.save(os.path.join(data_dir, "result.tiff"), options) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.tiff")) | |
# Create WEBP image using the first frame | |
with WebPImage(frames[0]) as image: | |
# Add frames to the WEBP image using the AddPage method | |
for index in range(1, len(frames)): | |
image.add_page(frames[index]) | |
# Save WEBP image | |
image.save(os.path.join(data_dir, "result.webp")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.webp")) | |
# Determine frame size | |
framesize = frames[0].size | |
# Create APNG image | |
with ApngImage(ApngOptions(), framesize.width, framesize.height) as image: | |
# Add frames to the APNG image using the AddPage method | |
for index in range(1, len(frames)): | |
image.add_page(frames[index]) | |
# Save APNG image | |
image.save(os.path.join(data_dir, "result.png")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.png")) | |
# Determine frame size | |
framesize = frames[0].size | |
# Create DICOM image | |
with DicomImage(DicomOptions(), framesize.width, framesize.height) as image: | |
# Add frames to the APNG image using the AddPage method | |
for index in range(1, len(frames)): | |
image.add_page(frames[index]) | |
# Remove default empty page | |
image.remove_page(0) | |
# Save DICOM image | |
image.save(os.path.join(data_dir, "result.dcm")) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.dcm")) | |
for image in frames: | |
# just call __exit__() for disposing the resources | |
with image: | |
pass |
Create multipage image from vector images
In order to use vector images as animation frames you need to rasterize them first. The following source code sample shows how to create TIFF image using vector images:
import aspose.pycore as aspycore | |
from aspose.imaging import RasterImage, Image | |
from aspose.imaging.fileformats.tiff import TiffImage, TiffFrame | |
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.imaging.imageoptions import TiffOptions, PngOptions, SvgRasterizationOptions | |
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 | |
data_dir = templates_folder | |
def multipage_from_vector(): | |
# Rasterize vector images | |
rasterize_svg_to_png(os.path.join(data_dir, "template.svg"), os.path.join(data_dir, "result.png")) | |
rasterize_svg_to_png(os.path.join(data_dir, "template.svg"), os.path.join(data_dir, "result2.png")) | |
rasterize_svg_to_png(os.path.join(data_dir, "template.svg"), os.path.join(data_dir, "result3.png")) | |
# Load frames | |
frames = load_frames() | |
# Create TIFF image using the first frame | |
with TiffImage(TiffFrame(frames[0])) as image: | |
# Add frames to the TIFF image using the AddPage method | |
for index in range(1, len(frames)): | |
image.add_page(frames[index]) | |
# Save TIFF image using options | |
options = TiffOptions(TiffExpectedFormat.TIFF_JPEG_RGB) | |
image.save(os.path.join(data_dir, "result.tiff"), options) | |
for image in frames: | |
# just call __exit__() for disposing the resources | |
with image: | |
pass | |
def rasterize_svg_to_png(input_path, output_path): | |
# Load vector image | |
with Image.load(input_path) as image: | |
# Save PNG image | |
obj_init = SvgRasterizationOptions() | |
obj_init.page_width = float(image.width) | |
obj_init.page_height = float(image.height) | |
obj_init2 = PngOptions() | |
obj_init2.vector_rasterization_options = obj_init | |
image.save(output_path, obj_init2) | |
def load_frames(): | |
out_list = list() | |
for file_path in ["result.png", "result2.png", "result3.png"]: | |
out_list.append(aspycore.as_of(Image.load(os.path.join(data_dir, file_path)), RasterImage)) | |
return out_list | |
# Run | |
multipage_from_vector() | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.tiff")) | |
os.remove(os.path.join(data_dir, "result.png")) | |
os.remove(os.path.join(data_dir, "result2.png")) | |
os.remove(os.path.join(data_dir, "result3.png")) |
Create animation from an array of images
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.imaging.imageoptions import TiffOptions | |
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 | |
data_dir = templates_folder | |
files = ["template.tiff", "template.gif", "template.png"] | |
images = [] | |
for file in files: | |
file_path = os.path.join(data_dir, file) | |
images.append(Image.load(file_path)) | |
with Image.create(images, True) as image: | |
image.save(os.path.join(data_dir, "result.tiff"), TiffOptions(TiffExpectedFormat.TIFF_JPEG_RGB)) | |
for image in images: | |
# just call __exit__() for disposing the resources | |
with image: | |
pass | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.tiff")) |