Package: apngasm-python
apngasm-pythonA nanobind API forapngasm, a tool/library for APNG assembly/disassembly.apngasm is originally a CLI program for quickly assembling PNG images into animated PNG (APNG). It also supports creating compressed APNG.apngasm-python is a binding for apngasm using nanobind, allowing you to use apngasm without calling it using commands.With this module, you can even create APNG using images inside memory (No need to write them out as file and call apngasm! This is about 2 times faster from testing.)A similar python module ishttps://github.com/eight04/pyAPNG, which handles APNG files with python natively and does not support compression.For convenience, prebuilt library is packaged with this module, so you need not download apngasm.Documentations:https://apngasm-python.readthedocs.io/en/latest/Installpip install apngasm-pythonOptionally, you can also installPillowandnumpypip install Pillow numpyExample usageThe recommended usage is tofrom apngasm_python.apngasm import APNGAsmBinder, seeexample/example_binder.pyfromapngasm_python.apngasmimportAPNGAsmBinderimportnumpyasnpfromPILimportImageimportosapngasm=APNGAsmBinder()# From fileforfile_nameinsorted(os.listdir('frames')):# To adjust frame duration, set delay_num and delay_den# The frame duration will be (delay_num / delay_den) secondsapngasm.add_frame_from_file(file_path=os.path.join('frames',file_name),delay_num=100,delay_den=1000)# Default value of loops is 0, which is infinite looping of APNG animation# This sets the APNG animation to loop for 3 times before stoppingapngasm.set_loops(3)apng.assemble('result-from-file.apng')apngasm.reset()# From Pillowforfile_nameinsorted(os.listdir('frames')):image=Image.open(os.path.join('frames',file_name)).convert('RGBA')frame=apngasm.add_frame_from_pillow(image,delay_num=50,delay_den=1000)apngasm.assemble('result-from-pillow.apng')apngasm.reset()# Disassemble and get pillow image of one frame# You can use with statement to avoid calling reset()withAPNGAsmBinder()asapng:frames=apng.disassemble_as_pillow('input/ball.apng')frame=frames[0]frame.save('output/ball0.png')# Disassemble all APNG into PNGsapngasm.save_pngs('output')Alternatively, you can reduce overhead and do advanced tasks by calling methods directly, seeexample/example_direct.pyfromapngasm_python._apngasm_pythonimportAPNGAsm,APNGFrame,create_frame_from_rgb,create_frame_from_rgbaimportnumpyasnpfromPILimportImageimportosapngasm=APNGAsm()# From fileforfile_nameinsorted(os.listdir('frames')):# To adjust frame duration, set delay_num and delay_den# The frame duration will be (delay_num / delay_den) secondsapngasm.add_frame_from_file(file_path=os.path.join('frames',file_name),delay_num=100,delay_den=1000)# Default value of loops is 0, which is infinite looping of APNG animation# This sets the APNG animation to loop for 3 times before stoppingapngasm.set_loops(3)apng.assemble('result-from-file.apng')# From Pillowapngasm.reset()forfile_nameinsorted(os.listdir('frames')):image=Image.open(os.path.join('frames',file_name)).convert('RGBA')frame=create_frame_from_rgba(np.array(image),image.width,image.height)frame.delay_num=50frame.delay_den=1000apngasm.add_frame(frame)apngasm.assemble('result-from-pillow.apng')# Disassemble and get pillow image of one frameapngasm.reset()frames=apngasm.disassemble('input/ball.apng')frame=frames[0]im=Image.frombytes(mode,(frame.width,frame.height),frame.pixels)im.save('output/ball0.png')# Disassemble all APNG into PNGsapngasm.save_pngs('output')The methods are based onapngasm.handapngframe.hYou can get more info about the binding fromsrc/apngasm_python.cpp, or by...fromapngasm_pythonimport_apngasm_pythonhelp(_apngasm_python)Building from sourcegitclonehttps://github.com/laggykiller/apngasm-python.gitcdapngasm-python
gitsubmoduleupdate--init--recursive# To build wheelpython3-mbuild.# To install directlypip3install.To cross-compile, please set environment variables:# Choose only one# On Windows (cmd, not PowerShell)setAPNGASM_COMPILE_TARGET=x86_64setAPNGASM_COMPILE_TARGET=x86setAPNGASM_COMPILE_TARGET=armv8# On *nixexportAPNGASM_COMPILE_TARGET=x64exportAPNGASM_COMPILE_TARGET=x86exportAPNGASM_COMPILE_TARGET=armv8exportAPNGASM_COMPILE_TARGET=ppc64leexportAPNGASM_COMPILE_TARGET=s390xCreditsapngasm:https://github.com/apngasm/apngasmPackaging:https://github.com/tttapa/py-build-cmakeExample files:https://apng.onevcat.com/demo/https://commons.wikimedia.org/wiki/File:Animated_PNG_example_bouncing_beach_ball.pnghttps://commons.wikimedia.org/wiki/File:Grayscale_8bits_palette_sample_image.pnghttps://commons.wikimedia.org/wiki/File:RG_16bits_palette_sample_image.png