Pure Python library for saving and loading PNG images without external dependencies
npx @tessl/cli install tessl/pypi-pypng@0.20220715.0A pure Python library for reading and writing PNG images without external dependencies. PyPNG provides complete coverage of PNG formats supporting all allowable bit depths (1/2/4/8/16/24/32/48/64 bits per pixel) and color combinations including greyscale, RGB, RGBA, and LA (greyscale with alpha) formats.
pip install pypngimport pngimport png
# Create a simple PNG from array data
png.from_array([[255, 0, 0, 255],
[0, 255, 255, 0]], 'L').save("small_smiley.png")
# Read a PNG file
reader = png.Reader(filename='image.png')
width, height, rows, info = reader.read()
# Write a PNG file using Writer
writer = png.Writer(width=256, height=256, greyscale=False)
with open('output.png', 'wb') as f:
writer.write_array(f, pixel_data)PyPNG follows a clean separation between reading and writing operations:
This design provides both high-level convenience methods and low-level control over PNG creation and parsing, making it suitable for simple image tasks and complex PNG manipulation workflows. PyPNG also includes a comprehensive suite of command-line tools for PNG manipulation and conversion tasks.
Comprehensive PNG decoding with format conversion, chunk access, and metadata extraction. Supports all PNG color types and bit depths with automatic format detection.
class Reader:
def __init__(self, _guess=None, filename=None, file=None, bytes=None): ...
def read(self, lenient=False): ...
def read_flat(self): ...
def asRGB(self): ...
def asRGBA(self): ...
def asRGB8(self): ...
def asRGBA8(self): ...
def asDirect(self): ...
def chunk(self, lenient=False): ...
def chunks(self): ...
def preamble(self, lenient=False): ...
def palette(self, alpha='natural'): ...Full PNG encoding with comprehensive format control, metadata support, and optimization options. Handles all PNG color types, bit depths, and optional chunks.
class Writer:
def __init__(self, width=None, height=None, size=None, greyscale=Default,
alpha=False, bitdepth=8, palette=None, transparent=None,
background=None, gamma=None, compression=None, interlace=False,
planes=None, colormap=None, maxval=None, chunk_limit=2**20,
x_pixels_per_unit=None, y_pixels_per_unit=None, unit_is_meter=False): ...
def write(self, outfile, rows): ...
def write_array(self, outfile, pixels): ...
def write_packed(self, outfile, rows): ...
def write_passes(self, outfile, rows): ...Convenient image data container with automatic format handling and multiple output methods for easy PNG manipulation workflows.
class Image:
def save(self, file): ...
def write(self, file): ...
def stream(self): ...Simple functions for quick PNG creation and manipulation without requiring detailed format knowledge or class instantiation.
def from_array(a, mode=None, info={}): ...
def write_chunks(out, chunks): ...Comprehensive suite of command-line utilities for PNG manipulation, format conversion, and image processing tasks. These tools can be used directly from the command line without programming.
# Command-line utilities available after installation:
# prichunkpng - Add/remove PNG chunks and metadata
# pricolpng - Join PNG images vertically (column)
# priditherpng - Apply dithering to PNG images
# priforgepng - Generate test patterns and synthetic images
# prigreypng - Convert PNG to greyscale
# pripalpng - Extract/manipulate PNG palettes
# pripamtopng - Convert NetPBM PAM/PNM files to PNG
# priplan9topng - Convert Plan 9 image format to PNG
# pripnglsch - PNG file analysis and validation
# pripngtopam - Convert PNG to NetPBM PAM/PNM files
# prirowpng - Join PNG images horizontally (row)
# priweavepng - Advanced PNG channel manipulation and weavingclass Error(Exception): ...
class FormatError(Error): ...
class ProtocolError(Error): ...
class ChunkError(FormatError): ...import collections
Resolution = collections.namedtuple('_Resolution', 'x y unit_is_meter')
class Default:
"""Sentinel class for default greyscale parameter"""__version__: str = "0.20220715.0"
signature: bytes # PNG file signature
adam7: tuple # Adam7 interlacing pattern coordinates
fromarray = from_array # Alternative name for from_array