Creating and manipulating animation using APNG fileformat

Overview

The Animated Portable Network Graphics (APNG) file format is an extension to the Portable Network Graphics (PNG) specification. It allows for animated PNG files that work similarly to animated GIF files, while supporting 24-bit images and 8-bit transparency not available for GIFs. It also retains backward compatibility with non-animated PNG files.

1) Export APNG animation to animated GIF

from aspose.imaging import Image
from aspose.imaging.imageoptions import GifOptions
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.png")) as image:
# Save to the same format
image.save(os.path.join(data_dir, "result.png"))
# Export to the other animated format
image.save(os.path.join(data_dir, "result.gif"), GifOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))
os.remove(os.path.join(data_dir, "result.gif"))
Input image Result image
Input image Result image

2) Create an animation from multi-page Tiff file

import aspose.pycore as aspycore
from aspose.imaging import *
from aspose.imaging.imageoptions import ApngOptions
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:
# Setting up the default frame duration
obj_init = ApngOptions()
obj_init.default_frame_time = 500
image.save(os.path.join(data_dir, "result.png"), obj_init)
obj_init2 = ApngOptions()
obj_init2.default_frame_time = 250
image.save(os.path.join(data_dir, "result2.png"), obj_init2)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))
os.remove(os.path.join(data_dir, "result2.png"))

Source image - img4.tif

Result animation:

todo:Apng animation example

3) Create APNG animation from single-page image

import aspose.pycore as aspycore
from aspose.imaging import RasterImage, Image
from aspose.imaging.fileformats.apng import ApngImage, ApngFrame
from aspose.imaging.fileformats.png import PngColorType
from aspose.imaging.imageoptions import ApngOptions
from aspose.imaging.sources import FileCreateSource
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
animation_duration = 1000
frame_duration = 70
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.png")), RasterImage) as source_image:
with ApngOptions() as create_options:
create_options.source = FileCreateSource(os.path.join(data_dir, "result.png"), False)
create_options.default_frame_time = frame_duration
create_options.color_type = PngColorType.TRUECOLOR_WITH_ALPHA
with aspycore.as_of(Image.create(create_options, source_image.width, source_image.height), ApngImage) as apng_image:
num_of_frames = animation_duration / frame_duration
num_of_frames2 = num_of_frames / 2
apng_image.remove_all_frames()
# add first frame
apng_image.add_frame(source_image, frame_duration)
# add intermediate frames
# for loop
for_first_step2 = True
frame_index = 1
while frame_index < num_of_frames - 1:
if for_first_step2:
for_first_step2 = False
else:
frame_index += 1
if not (frame_index < num_of_frames - 1):
break
print("add frame")
apng_image.add_frame(source_image, frame_duration)
last_frame = aspycore.as_of(apng_image.pages[apng_image.page_count - 1], ApngFrame)
gamma = num_of_frames - frame_index - 1 if frame_index >= num_of_frames2 else frame_index
last_frame.adjust_gamma(gamma)
# add last frame
apng_image.add_frame(source_image, frame_duration)
apng_image.save()
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))
todo:image_alt_text todo:image_alt_text
Source image Created animation

4) Create APNG animation using vector graphics

Created from graphics animation: