11#!/usr/bin/env python3
22
3- """PPM and PGM image files reading, displaying and writing for Python > 3.10 .
4- ------------------------------------------------------------------------------
3+ """PPM and PGM image files reading, displaying and writing for Python >= 3.11 .
4+ -------------------------------------------------------------------------------
55
66Overview
77---------
88
99PyPNM module is a pack of functions for dealing with PPM and PGM image files.
1010Functions included are:
1111
12- - `pnm2list`: reading binary or ascii RGB PPM or L PGM file and returning image data
12+ - `pnm2list`: reading binary or ASCII RGB PPM or L PGM file and returning image data
1313as nested list of int.
1414- `list2bin`: getting image data as nested list of int and creating binary PPM (P6) or PGM (P5)
15- data structure in memory. Suitable for generating data to display with
16- Tkinter `PhotoImage(data=...)` class.
15+ data structure in memory. Suitable for generating data to display with Tkinter `PhotoImage (data=...)` class.
1716- `list2pnmbin`: getting image data as nested list of int and writing binary PPM (P6) or PGM (P5) image file.
18- Note that bytes generations procedure is optimized to save memory while working with large files and
19- therefore is different from that used in `list2bin`.
20- - `list2pnmascii`: alternative function to write ASCII PPM (P3) or PGM (P2) files.
17+ Note that bytes generations procedure is different from that used in `list2bin`.
18+ - `list2pnmascii`: getting image data as nested list of int and writing ASCII PPM (P3) or PGM (P2) files.
2119- `list2pnm`: getting image data as nested list of int and writing either binary or ASCII PNM
2220depending on `bin` argument value.
2321- `create_image`: creating empty nested 3D list for image representation.
24- Not used within this particular module but often needed by programs this module is supposed to be used with.
2522
2623Installation
2724-------------
7067with Tkinter `PhotoImage` class.
7168
7269May be freely used, redistributed and modified.
73- In case of introducing useful modifications, report to the developer at once.
70+
71+ In case of introducing useful modifications, report upstairs at once.
7472
7573References
7674-----------
7775
78761. Netpbm specifications: https://netpbm.sourceforge.net/doc/
79- 2. PyPNM at GitHub: https://github.com/Dnyarri/PyPNM/
80- 3. PyPNM at PyPI: https://pypi.org/project/PyPNM/
81- 4. PyPNM Documentation: https://dnyarri.github.io/pypnm/pypnm.pdf
77+ 2. PyPNM for Python >= 3.11 at GitHub: https://github.com/Dnyarri/PyPNM/
78+ 3. PyPNM for Python >= 3.4 at GitHub: https://github.com/Dnyarri/PyPNM/tree/py34
79+ 4. PyPNM at PyPI: https://pypi.org/project/PyPNM/
80+ 5. PyPNM Documentation: https://dnyarri.github.io/pypnm/pypnm.pdf
8281
8382"""
8483
8584__author__ = 'Ilya Razmanov'
8685__copyright__ = '(c) 2024-2025 Ilya Razmanov'
8786__credits__ = 'Ilya Razmanov'
8887__license__ = 'unlicense'
89- __version__ = '2.21.2.2 '
88+ __version__ = '2.21.3.12 '
9089__maintainer__ = 'Ilya Razmanov'
9190__email__ = 'ilyarazmanov@gmail.com'
9291__status__ = 'Production'
@@ -112,7 +111,8 @@ def pnm2list(in_filename: str) -> tuple[int, int, int, int, list[list[list[int]]
112111 for reading data from PPM/PGM, where:
113112
114113 - `X`, `Y`, `Z`: image dimensions (int);
115- - `maxcolors`: maximum of color per channel for current image (int), 255 for 8 bit and 65535 for 16 bit input. Note that 1 bit images get promoted to 8 bit L upon import;
114+ - `maxcolors`: maximum of color per channel for current image (int),
115+ 255 for 8 bit and 65535 for 16 bit input. Note that 1 bit images get promoted to 8 bit L upon import.
116116 - `list_3d`: image pixel data as list(list(list(int)));
117117 - `in_filename`: PPM/PGM file name (str).
118118
@@ -345,7 +345,7 @@ def _p1(in_filename: str) -> tuple[int, int, int, int, list[list[list[int]]]]:
345345 ╚══════════╝ """
346346
347347def list2bin (list_3d : list [list [list [int ]]], maxcolors : int , show_chessboard : bool = False ) -> bytes :
348- """Convert nested image data list to PGM P5 or PPM P6 (binary) data structure in memory to be used with Tkinter PhotoImage(data=...) .
348+ """Convert nested image data list to PGM P5 or PPM P6 bytes in memory.
349349
350350 Usage:
351351
@@ -355,7 +355,9 @@ def list2bin(list_3d: list[list[list[int]]], maxcolors: int, show_chessboard: bo
355355
356356 - `list_3d`: Y * X * Z list (image) of lists (rows) of lists (pixels) of ints (channel values);
357357 - `maxcolors`: maximum of color per channel for current image (int);
358- - `show_chessboard`: optional bool, set `True` to show LA and RGBA images against chessboard pattern; `False` or missing show existing L or RGB data for transparent areas as opaque. Default is `False` for backward compatibility.
358+ - `show_chessboard`: optional bool, set `True` to show LA and RGBA images against chessboard pattern;
359+ `False` or missing show existing L or RGB data for transparent areas as fully opaque.
360+ Default is `False` for backward compatibility.
359361 - `image_bytes`: PNM-structured binary data.
360362
361363 """
@@ -523,7 +525,7 @@ def list2pnm(out_filename: str, list_3d: list[list[list[int]]], maxcolors: int,
523525 ╚════════════════════╝ """
524526
525527def create_image (X : int , Y : int , Z : int ) -> list [list [list [int ]]]:
526- """Create empty 3D nested list of X * Y * Z size."""
528+ """Create 3D nested list of X * Y * Z size filled with zeroes ."""
527529
528530 new_image = [[[0 for z in range (Z )] for x in range (X )] for y in range (Y )]
529531
@@ -532,4 +534,8 @@ def create_image(X: int, Y: int, Z: int) -> list[list[list[int]]]:
532534
533535# ↓ Dummy stub for standalone execution attempt
534536if __name__ == '__main__' :
535- print (f'Module PyPNM { __version__ } to be imported, not run as standalone!' )
537+ print ('Module to be imported, not run as standalone.' )
538+ need_help = input ('Would you like to read some help (y/n)?' )
539+ if need_help .startswith (('y' , 'Y' )):
540+ import pnmlpnm
541+ help (pnmlpnm )
0 commit comments