|
8 | 8 | Joint between PyPNG and other programs |
9 | 9 | -------------------------------------- |
10 | 10 |
|
11 | | -Created by: `Ilya Razmanov<mailto:ilyarazmanov@gmail.com>`_ |
12 | | -aka `Ilyich the Toad<mailto:amphisoft@gmail.com>`_. |
13 | | -
|
14 | 11 | Overview |
15 | | ---------- |
| 12 | +-------- |
16 | 13 |
|
17 | 14 | **pnglpng** (png-list-png) is a suitable joint between `PyPNG`_ |
18 | 15 | and other Python programs, providing data conversion from/to used by PyPNG |
19 | 16 | to/from understandable by ordinary average human. |
20 | 17 |
|
21 | 18 | Functions included are: |
22 | 19 |
|
23 | | -:png2list: reading PNG file and returning all data; |
24 | | -:list2png: getting data and writing PNG file; |
25 | | -:create_image: creating empty nested 3D list for image representation. |
| 20 | +- ``png2list``: reading PNG file and returning all data; |
| 21 | +- ``list2png``: getting data and writing PNG file; |
| 22 | +- ``create_image``: creating empty nested 3D list for image representation. |
26 | 23 |
|
27 | 24 | Installation |
28 | | -------------- |
| 25 | +------------ |
29 | 26 |
|
30 | 27 | Should be kept together with ``png.py`` module. See ``import`` for detail. |
31 | 28 |
|
32 | 29 | Usage |
33 | | ------- |
34 | | -
|
35 | | -After ``import pnglpng``, use something like |
| 30 | +----- |
36 | 31 |
|
37 | | -:: |
| 32 | +After ``import pnglpng``, use something like:: |
38 | 33 |
|
39 | 34 | X, Y, Z, maxcolors, list_3d, info = pnglpng.png2list(in_filename) |
40 | 35 |
|
41 | | -for reading data from ``in_filename`` PNG, where: |
42 | | -
|
43 | | -:X, Y, Z: image dimensions (int); |
44 | | -:maxcolors: number of colors per channel for current image (int); |
45 | | -:list_3d: image pixel data as list(list(list(int))); |
46 | | -:info: PNG chunks like resolution etc (dictionary); |
| 36 | +for reading data from PNG file, where: |
47 | 37 |
|
48 | | -and |
| 38 | +- ``X``, ``Y``, ``Z``: PNG image dimensions (int); |
| 39 | +- ``maxcolors``: number of colors per channel for current image (int), either 1, or 255, or 65535, for 1 bpc, 8 bpc and 16 bpc PNG respectively; |
| 40 | +- ``list_3d``: Y * X * Z list (image) of lists (rows) of lists (pixels) of ints (channels), from PNG iDAT; |
| 41 | +- ``info``: dictionary of PNG chunks like resolution etc., as they are accessible by PyPNG. |
49 | 42 |
|
50 | | -:: |
| 43 | +and :: |
51 | 44 |
|
52 | 45 | pnglpng.list2png(out_filename, list_3d, info) |
53 | 46 |
|
54 | | -for writing data to ``out_filename`` PNG. |
| 47 | +for writing data as listed above to ``out_filename`` PNG. |
55 | 48 |
|
56 | | -Prerequisites and References |
57 | | ----------------------------- |
| 49 | +References |
| 50 | +---------- |
58 | 51 |
|
59 | | -1. `PyPNG`_ |
| 52 | +1. `PyPNG`_ download |
60 | 53 | 2. `PyPNG docs`_ |
61 | 54 |
|
62 | 55 | .. _PyPNG: https://gitlab.com/drj11/pypng |
|
84 | 77 | def png2list(in_filename): |
85 | 78 | """Take PNG filename and return PNG data in a human-friendly form. |
86 | 79 |
|
87 | | - Usage |
88 | | -
|
89 | | - :: |
90 | | -
|
91 | | - X, Y, Z, maxcolors, list_3d, info = pnglpng.png2list(in_filename) |
| 80 | + :param str in_filename: input file name; |
| 81 | + :return X, Y, Z, maxcolors, list_3d, info: tuple, consisting of: |
92 | 82 |
|
93 | | - Takes PNG filename ``in_filename`` and return the following tuple |
94 | | -
|
95 | | - :X, Y, Z: PNG image dimensions (int); |
96 | | - :maxcolors: number of colors per channel for current image (int), |
97 | | - either 1, or 255, or 65535, for 1 bpc, 8 bpc and 16 bpc PNG respectively; |
98 | | - :list_3d: Y * X * Z list (image) of lists (rows) of lists (pixels) of ints (channels), from PNG iDAT; |
99 | | - :info: dictionary from PNG chunks like resolution etc. as they are accessible by PyPNG. |
| 83 | + - ``X``, ``Y``, ``Z``: PNG image dimensions (int); |
| 84 | + - ``maxcolors``: number of colors per channel for current image (int), either 1, or 255, or 65535, for 1 bpc, 8 bpc and 16 bpc PNG respectively; |
| 85 | + - ``list_3d``: Y * X * Z list (image) of lists (rows) of lists (pixels) of ints (channels), from PNG iDAT; |
| 86 | + - ``info``: dictionary of PNG chunks like resolution etc., as they are accessible by PyPNG. |
100 | 87 |
|
101 | 88 | """ |
102 | 89 |
|
@@ -128,18 +115,12 @@ def png2list(in_filename): |
128 | 115 | def list2png(out_filename, list_3d, info): |
129 | 116 | """Take filename and image data, and create PNG file. |
130 | 117 |
|
131 | | - Usage |
132 | | -
|
133 | | - :: |
134 | | -
|
135 | | - pnglpng.list2png(out_filename, list_3d, info) |
136 | | -
|
137 | | - Take data described below and write PNG file `out_filename` out of it. |
138 | | -
|
139 | | - :list_3d: Y * X * Z list (image) of lists (rows) of lists (pixels) of ints (channels); |
140 | | - :info: dictionary, chunks like resolution etc. as you want them to be present in PNG. |
| 118 | + :param list_3d: Y * X * Z list (image) of lists (rows) of lists (pixels) of ints (channels); |
| 119 | + :param info: dictionary, chunks like resolution etc. as you want them to be present in PNG; |
| 120 | + :param str out_filename: output PNG file name (str). |
141 | 121 |
|
142 | | - Note that ``X``, ``Y`` and ``Z`` detected from the list structure override those set in ``info``. |
| 122 | + .. note:: ``X``, ``Y`` and ``Z`` detected from the list structure override those set in ``info``. |
| 123 | + .. warning:: Correct ``info['bitdepth']`` is **critical** because it cannot be detected from the list structure. |
143 | 124 |
|
144 | 125 | """ |
145 | 126 |
|
@@ -177,7 +158,7 @@ def flatten_2d(list_3d): |
177 | 158 |
|
178 | 159 | yield from ([list_3d[y][x][z] for x in range(X) for z in range(Z)] for y in range(Y)) |
179 | 160 |
|
180 | | - # ↓ Writing PNG with `.write` method (row by row), |
| 161 | + # ↓ Writing PNG with `.write` method (row by row), |
181 | 162 | # using `flatten_2d` generator to save memory |
182 | 163 | writer = png.Writer(X, Y, **info) |
183 | 164 | with open(out_filename, 'wb') as result_png: |
|
0 commit comments