pyAPNG API reference¶
A Python module to deal with APNG file.
Usage/examples can be founded at pyAPNG’s readme.
Functions¶
-
parse_chunks(b)¶ Parse PNG bytes into multiple chunks.
-
make_chunk(chunk_type, chunk_data)¶ Create a raw chunk by composing chunk type and data. It calculates chunk length and CRC for you.
-
make_text_chunk(type='tEXt', key='Comment', value='', compression_flag=0, compression_method=0, lang='', translated_key='')¶ Create a text chunk with a key value pair. See https://www.w3.org/TR/PNG/#11textinfo for text chunk information.
Usage:
from apng import APNG, make_text_chunk im = APNG.open("file.png") png, control = im.frames[0] png.chunks.append(make_text_chunk("tEXt", "Comment", "some text")) im.save("file.png")
- Parameters
type (str) –
Text chunk type: “tEXt”, “zTXt”, or “iTXt”:
tEXt uses Latin-1 characters. zTXt uses Latin-1 characters, compressed with zlib. iTXt uses UTF-8 characters.
key (str) – The key string, 1-79 characters.
value (str) – The text value. It would be encoded into
bytesand compressed if needed.compression_flag (int) – The compression flag for iTXt.
compression_method (int) – The compression method for zTXt and iTXt.
lang (str) – The language tag for iTXt.
translated_key (str) – The translated keyword for iTXt.
- Return type
Classes¶
-
class
Chunk¶ A namedtuple to represent the PNG chunk.
- Parameters
Create new instance of Chunk(type, data)
-
class
PNG¶ Represent a PNG image.
-
chunks= []¶ A list of
Chunk. After reading a PNG file, the bytes are parsed into multiple chunks. You can remove/add chunks into this array before callingto_bytes().
-
classmethod
open(file)¶ Open a PNG file.
- Parameters
file (path-like or file-like) – Input file.
- Return type
-
classmethod
open_any(file)¶ Open an image file. If the image is not PNG format, it would convert the image into PNG with Pillow module. If the module is not installed,
ImportErrorwould be raised.- Parameters
file (path-like or file-like) – Input file.
- Return type
-
save(file)¶ Save the entire image to a file.
- Parameters
file (path-like or file-like) – Output file.
-
-
class
FrameControl(width=None, height=None, x_offset=0, y_offset=0, delay=100, delay_den=1000, depose_op=1, blend_op=0)¶ A data class holding fcTL info.
Parameters are assigned as object members. See https://wiki.mozilla.org/APNG_Specification for the detail of fcTL.
-
class
APNG(num_plays=0)¶ Represent an APNG image.
An
APNGis composed by multiplePNGs andFrameControl, which can be inserted withappend().- Parameters
num_plays (int) – Number of times to loop. 0 = infinite.
- Variables
frames (list[tuple(PNG, FrameControl)]) – The frames of APNG.
num_plays (int) – same as
num_plays.
-
append(png, **options)¶ Append one frame.
- Parameters
options (dict) – The options for
FrameControl.
-
append_file(file, **options)¶ Create a PNG from file and append the PNG as a frame.
- Parameters
file (path-like or file-like.) – Input file.
options (dict) – The options for
FrameControl.
-
classmethod
from_bytes(b)¶ Create an APNG from raw bytes.
-
classmethod
from_files(files, **options)¶ Create an APNG from multiple files.
This is a shortcut of:
im = APNG() for file in files: im.append_file(file, **options)
- Parameters
files (list) – A list of filename. See
PNG.open().options (dict) – Options for
FrameControl.
- Return type
-
classmethod
open(file)¶ Open an APNG file.
- Parameters
file (path-like or file-like.) – Input file.
- Return type
-
save(file)¶ Save the entire image to a file.
- Parameters
file (path-like or file-like) – Output file.