png API¶
png
¶
Zero-dependency PNG and BMP image codec with matrix compression API.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Encode and decode PNG (via stdlib zlib) and BMP images using only the
standard library. Provides a shared Image data structure for format
conversion and a matrix_to_png / png_to_matrix API that exploits
PNG row filters for efficient 2-D numeric data compression.
Supported PNG features
- Color types: grayscale (0), RGB (2), grayscale+alpha (4), RGBA (6)
- Bit depths: 8 and 16 per channel
- All five adaptive row filters (None/Sub/Up/Average/Paeth)
- tEXt ancillary chunks (read and write)
Supported BMP features
- 24-bit RGB and 32-bit RGBA, uncompressed
- Bottom-up and top-down row order
Not supported (by design): - Adam7 interlacing, palette PNG (color type 3) - BMP RLE compression, BMP palette modes
ImageError
¶
DecodeError
¶
Bases: ImageError
Raised when image decoding fails.
EncodeError
¶
Bases: ImageError
Raised when image encoding fails.
Image
dataclass
¶
Decoded image pixel data.
Attributes:
| Name | Type | Description |
|---|---|---|
width |
int
|
Image width in pixels. |
height |
int
|
Image height in pixels. |
data |
bytes
|
Raw pixel bytes in row-major order. |
mode |
str
|
Pixel format: |
bit_depth |
int
|
Bits per channel (8 or 16). |
metadata |
dict[str, str] | None
|
Optional dict of ancillary text data (PNG tEXt chunks). |
Source code in png/png.py
decode_png(source)
¶
Decode a PNG image into an :class:Image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | PathLike[str] | IO[bytes] | bytes
|
File path, file object, or raw PNG bytes. |
required |
Returns:
| Type | Description |
|---|---|
Image
|
Decoded image. |
Raises:
| Type | Description |
|---|---|
DecodeError
|
If the data is not valid PNG or uses unsupported features. |
Source code in png/png.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
encode_png(image, dest=None, *, filter_strategy='auto', compression_level=6)
¶
Encode an :class:Image as PNG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Image to encode. |
required |
dest
|
str | PathLike[str] | IO[bytes] | None
|
File path, file object, or |
None
|
filter_strategy
|
str
|
Row filter strategy — |
'auto'
|
compression_level
|
int
|
zlib compression level (0–9). |
6
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
PNG bytes when dest is |
Source code in png/png.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
decode_bmp(source)
¶
Decode a BMP image into an :class:Image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | PathLike[str] | IO[bytes] | bytes
|
File path, file object, or raw BMP bytes. |
required |
Returns:
| Type | Description |
|---|---|
Image
|
Decoded image. |
Raises:
| Type | Description |
|---|---|
DecodeError
|
If the data is not valid BMP or uses unsupported features. |
Source code in png/png.py
encode_bmp(image, dest=None)
¶
Encode an :class:Image as BMP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Image to encode (must be |
required |
dest
|
str | PathLike[str] | IO[bytes] | None
|
File path, file object, or |
None
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
BMP bytes when dest is |
Source code in png/png.py
convert(image, mode)
¶
Convert an :class:Image to a different pixel mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Source image. |
required |
mode
|
str
|
Target mode ( |
required |
Returns:
| Type | Description |
|---|---|
Image
|
New image in the target mode. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the conversion is not supported. |
Source code in png/png.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | |
matrix_to_png(matrix, dest=None, *, bit_depth=8, filter_strategy='auto', compression_level=6)
¶
Encode a 2-D numeric matrix as a grayscale PNG.
Integer values in [0, 2^bit_depth - 1] are stored directly.
Float values (or out-of-range integers) are linearly mapped to
the full pixel range; the mapping parameters are stored in a PNG
tEXt chunk so :func:png_to_matrix can reconstruct them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
list[list[int | float]] | list[list[Any]]
|
Rows of numeric values (all rows must have equal length). |
required |
dest
|
str | PathLike[str] | IO[bytes] | None
|
File path, file object, or |
None
|
bit_depth
|
int
|
8 or 16 bits per sample. |
8
|
filter_strategy
|
str
|
PNG row filter strategy. |
'auto'
|
compression_level
|
int
|
zlib compression level (0–9). |
6
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
PNG bytes when dest is |
Source code in png/png.py
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 | |
png_to_matrix(source)
¶
Decode a grayscale PNG into a 2-D numeric matrix.
If the PNG contains zerodep:matrix metadata (written by
:func:matrix_to_png), float values are reconstructed from the
stored scale and offset. Otherwise raw pixel values are returned
as integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | PathLike[str] | IO[bytes] | bytes
|
File path, file object, or raw PNG bytes. |
required |
Returns:
| Type | Description |
|---|---|
list[list[int]] | list[list[float]]
|
2-D list of numeric values. |