png 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- png-0.0.1/LICENSE +21 -0
- png-0.0.1/PKG-INFO +499 -0
- png-0.0.1/README.md +476 -0
- png-0.0.1/pyproject.toml +62 -0
- png-0.0.1/pyproject.toml.orig +61 -0
- png-0.0.1/src/png/__init__.py +107 -0
- png-0.0.1/src/png/_apng.py +68 -0
- png-0.0.1/src/png/_color_codes.py +12 -0
- png-0.0.1/src/png/_composite.py +105 -0
- png-0.0.1/src/png/_exif.py +155 -0
- png-0.0.1/src/png/_exif_binary.py +103 -0
- png-0.0.1/src/png/_exif_fields.py +254 -0
- png-0.0.1/src/png/_exif_values.py +71 -0
- png-0.0.1/src/png/_icc_collections.py +84 -0
- png-0.0.1/src/png/_icc_header.py +80 -0
- png-0.0.1/src/png/_icc_lut.py +142 -0
- png-0.0.1/src/png/_icc_misc.py +147 -0
- png-0.0.1/src/png/_icc_mpet.py +144 -0
- png-0.0.1/src/png/_icc_records.py +108 -0
- png-0.0.1/src/png/_icc_registry_data.py +3498 -0
- png-0.0.1/src/png/_icc_text.py +69 -0
- png-0.0.1/src/png/_language.py +74 -0
- png-0.0.1/src/png/_language_data.py +677 -0
- png-0.0.1/src/png/_limits.py +71 -0
- png-0.0.1/src/png/_metadata.py +75 -0
- png-0.0.1/src/png/_pixels.py +244 -0
- png-0.0.1/src/png/_profiles.py +232 -0
- png-0.0.1/src/png/_stream.py +25 -0
- png-0.0.1/src/png/_structure.py +163 -0
- png-0.0.1/src/png/_validation.py +6 -0
- png-0.0.1/src/png/animation.py +172 -0
- png-0.0.1/src/png/chunk.py +114 -0
- png-0.0.1/src/png/document.py +167 -0
- png-0.0.1/src/png/error.py +40 -0
- png-0.0.1/src/png/extensions.py +253 -0
- png-0.0.1/src/png/format.py +103 -0
- png-0.0.1/src/png/header.py +109 -0
- png-0.0.1/src/png/image.py +471 -0
- png-0.0.1/src/png/limits.py +52 -0
- png-0.0.1/src/png/metadata.py +728 -0
- png-0.0.1/src/png/py.typed +0 -0
png-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TakWolf
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
png-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: png
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Pure Python Implementation of Portable Network Graphics (PNG) Specification and Extensions
|
|
5
|
+
Keywords: png,image
|
|
6
|
+
Author: TakWolf
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Maintainer: TakWolf
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Project-URL: Homepage, https://github.com/TakWolf/png-python
|
|
20
|
+
Project-URL: Source, https://github.com/TakWolf/png-python
|
|
21
|
+
Project-URL: Issues, https://github.com/TakWolf/png-python/issues
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# PNG - Python
|
|
25
|
+
|
|
26
|
+
[](https://www.python.org)
|
|
27
|
+
[](https://pypi.org/project/png/)
|
|
28
|
+
|
|
29
|
+
Pure Python Implementation of [Portable Network Graphics (PNG) Specification and Extensions](https://www.libpng.org/pub/png/spec/),
|
|
30
|
+
with PNG/APNG reading, editing, writing, and image construction.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
pip install png
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Tutorial
|
|
39
|
+
|
|
40
|
+
Use `PngImage` for decoded pixels, palettes, metadata, and animation. Use
|
|
41
|
+
`PngDocument` for an ordered list of raw chunks when editing a file without
|
|
42
|
+
re-encoding its pixel data. Objects expose mutable attributes; construct or edit
|
|
43
|
+
them directly, then call `save()` or `dump_to_bytes()` to write the result.
|
|
44
|
+
|
|
45
|
+
### Create an RGBA image
|
|
46
|
+
|
|
47
|
+
This example creates a 2 × 2 image containing red, green, blue, and a partially
|
|
48
|
+
transparent white pixel:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from png import PngHeader, PngImage
|
|
52
|
+
|
|
53
|
+
image = PngImage(
|
|
54
|
+
header=PngHeader(dimensions=(2, 2)), # RGBA, 8 bits per sample by default
|
|
55
|
+
bitmap=[
|
|
56
|
+
[255, 0, 0, 255, 0, 255, 0, 255],
|
|
57
|
+
[0, 0, 255, 255, 255, 255, 255, 128],
|
|
58
|
+
],
|
|
59
|
+
)
|
|
60
|
+
image.save('created.png')
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`bitmap` is a list of rows, from top to bottom. Each row is a flat list of integer
|
|
64
|
+
samples, from left to right; RGBA pixels use `[r, g, b, a, ...]`. Alpha is straight
|
|
65
|
+
(unassociated): zero is transparent and the maximum sample value is opaque.
|
|
66
|
+
|
|
67
|
+
| `PngColorType` | Samples per pixel | Supported bit depths |
|
|
68
|
+
| --- | --- | --- |
|
|
69
|
+
| `GRAYSCALE` | Gray | 1, 2, 4, 8, 16 |
|
|
70
|
+
| `TRUECOLOR` | R, G, B | 8, 16 |
|
|
71
|
+
| `INDEXED_COLOR` | Palette index | 1, 2, 4, 8 |
|
|
72
|
+
| `GRAYSCALE_ALPHA` | Gray, Alpha | 8, 16 |
|
|
73
|
+
| `TRUECOLOR_ALPHA` | R, G, B, Alpha | 8, 16 |
|
|
74
|
+
|
|
75
|
+
Every row has `width * header.channels` integers. Color samples range from zero
|
|
76
|
+
to `(1 << bit_depth) - 1`; palette indices select entries from `image.palette`.
|
|
77
|
+
Decoded samples retain the stored bit depth, including 16-bit values.
|
|
78
|
+
|
|
79
|
+
### Read and modify pixels
|
|
80
|
+
|
|
81
|
+
Continue with `created.png` from the previous example:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from png import PngImage
|
|
85
|
+
|
|
86
|
+
image = PngImage.load('created.png')
|
|
87
|
+
print(image.dimensions) # (2, 2)
|
|
88
|
+
print(image.header.bit_depth) # 8
|
|
89
|
+
print(image.bitmap[0][:4]) # [255, 0, 0, 255]
|
|
90
|
+
|
|
91
|
+
# Replace the top-left RGBA pixel with opaque yellow.
|
|
92
|
+
image.bitmap[0][0:4] = [255, 255, 0, 255]
|
|
93
|
+
image.save('edited.png')
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
For other files, inspect `header.color_type` and `header.channels` before editing
|
|
97
|
+
samples. Changing the header's dimensions or color type also requires updating
|
|
98
|
+
the bitmap and any dependent palette, transparency, or animation data. Saving
|
|
99
|
+
validates the current model and re-encodes its samples.
|
|
100
|
+
|
|
101
|
+
### Work with bytes and streams
|
|
102
|
+
|
|
103
|
+
The same interfaces are available on `PngImage` and `PngDocument`:
|
|
104
|
+
|
|
105
|
+
| Input/output | Read | Write |
|
|
106
|
+
| --- | --- | --- |
|
|
107
|
+
| File path | `load(path)` | `save(path)` |
|
|
108
|
+
| Binary stream | `parse(stream)` | `dump(stream)` |
|
|
109
|
+
| Bytes | `parse(data)` | `dump_to_bytes()` |
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from io import BytesIO
|
|
113
|
+
from png import PngImage
|
|
114
|
+
|
|
115
|
+
image = PngImage.load('created.png')
|
|
116
|
+
data = image.dump_to_bytes()
|
|
117
|
+
restored = PngImage.parse(data)
|
|
118
|
+
|
|
119
|
+
stream = BytesIO()
|
|
120
|
+
restored.dump(stream)
|
|
121
|
+
stream.seek(0)
|
|
122
|
+
restored = PngImage.parse(stream)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`load()` reads a complete file and rejects content after IEND. `parse()` reads
|
|
126
|
+
one PNG and stops at IEND, leaving any following bytes in a caller-owned stream.
|
|
127
|
+
Neither `parse()` nor `dump()` closes that stream.
|
|
128
|
+
|
|
129
|
+
### Create grayscale and indexed images
|
|
130
|
+
|
|
131
|
+
Grayscale uses one sample per pixel. This example stores four 2-bit values:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from png import PngColorType, PngHeader, PngImage
|
|
135
|
+
|
|
136
|
+
gray = PngImage(
|
|
137
|
+
PngHeader((4, 1), bit_depth=2, color_type=PngColorType.GRAYSCALE),
|
|
138
|
+
bitmap=[[0, 1, 2, 3]],
|
|
139
|
+
transparent_color=(0,), # Optional tRNS: gray zero is fully transparent
|
|
140
|
+
)
|
|
141
|
+
gray.save('gray.png')
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
For indexed images, `bitmap` contains indices and `palette` contains 8-bit RGB
|
|
145
|
+
triples. `palette_alpha` supplies optional 8-bit alpha values; omitted entries
|
|
146
|
+
are opaque:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from png import PngColorType, PngHeader, PngImage
|
|
150
|
+
|
|
151
|
+
indexed = PngImage(
|
|
152
|
+
PngHeader((3, 1), bit_depth=2, color_type=PngColorType.INDEXED_COLOR),
|
|
153
|
+
bitmap=[[0, 1, 2]],
|
|
154
|
+
palette=[(255, 0, 0), (0, 255, 0), (0, 0, 255)],
|
|
155
|
+
palette_alpha=[255, 128, 0],
|
|
156
|
+
)
|
|
157
|
+
indexed.recalculate_histogram() # Optional: create/update hIST from used indices
|
|
158
|
+
indexed.save('indexed.png')
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
`transparent_color` can also hold an RGB triple for a truecolor image. Images
|
|
162
|
+
with an alpha channel store transparency in their samples instead of tRNS.
|
|
163
|
+
|
|
164
|
+
For reference samples whose depth cannot be stored directly in PNG, use the
|
|
165
|
+
source-depth factory:
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from png import PngColorType, PngImage
|
|
169
|
+
|
|
170
|
+
scaled = PngImage.from_samples(
|
|
171
|
+
dimensions=(3, 1),
|
|
172
|
+
bitmap=[[0, 2048, 4095]],
|
|
173
|
+
sample_depth=12,
|
|
174
|
+
color_type=PngColorType.GRAYSCALE,
|
|
175
|
+
)
|
|
176
|
+
print(scaled.header.bit_depth) # 16
|
|
177
|
+
print(scaled.find_metadata(b'sBIT')[0].bits) # (12,)
|
|
178
|
+
scaled.save('scaled.png')
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The factory scales into the smallest legal PNG depth and records the original
|
|
182
|
+
depth in sBIT. A tuple such as `(10, 10, 10, 8)` specifies separate RGBA source
|
|
183
|
+
depths. Palette indices use the ordinary constructor and are never scaled as
|
|
184
|
+
intensities.
|
|
185
|
+
|
|
186
|
+
### Add and edit metadata
|
|
187
|
+
|
|
188
|
+
Metadata objects live in `image.metadata`. Unicode text uses iTXt:
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from png import PngGamma, PngImage, PngInternationalText, PngText
|
|
192
|
+
|
|
193
|
+
image = PngImage.load('created.png')
|
|
194
|
+
image.metadata.extend(
|
|
195
|
+
[
|
|
196
|
+
PngText('Author', 'Example'),
|
|
197
|
+
PngInternationalText(
|
|
198
|
+
keyword='Description',
|
|
199
|
+
text='Image description',
|
|
200
|
+
language_tag='en',
|
|
201
|
+
translated_keyword='Description',
|
|
202
|
+
compressed=True,
|
|
203
|
+
),
|
|
204
|
+
PngGamma(45455), # Stored integer; gamma is exactly 45455/100000
|
|
205
|
+
]
|
|
206
|
+
)
|
|
207
|
+
image.save('with-metadata.png')
|
|
208
|
+
|
|
209
|
+
loaded = PngImage.load('with-metadata.png')
|
|
210
|
+
for entry in loaded.find_metadata(b'iTXt'):
|
|
211
|
+
print(entry.keyword, entry.text, entry.language_tag)
|
|
212
|
+
entry.text = 'Updated description'
|
|
213
|
+
loaded.save('updated-metadata.png')
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
`find_metadata()` returns references to the matching objects. Modify an existing
|
|
217
|
+
singleton such as gAMA rather than appending a duplicate. Remove an entry with
|
|
218
|
+
`image.metadata.remove(entry)`. Other structured types include color descriptions,
|
|
219
|
+
physical dimensions, timestamps, background colors, significant bits, and
|
|
220
|
+
suggested palettes.
|
|
221
|
+
|
|
222
|
+
To embed an existing ICC profile, supply its original bytes. This example needs
|
|
223
|
+
an RGB profile in `profile.icc`, because `created.png` is RGBA:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
from pathlib import Path
|
|
227
|
+
from png import PngIccProfile, PngImage
|
|
228
|
+
|
|
229
|
+
image = PngImage.load('created.png')
|
|
230
|
+
profile = PngIccProfile('RGB profile', Path('profile.icc').read_bytes())
|
|
231
|
+
profile.validate_profile_tags()
|
|
232
|
+
image.metadata.append(profile)
|
|
233
|
+
image.save('with-profile.png')
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Exif similarly uses `PngExif(profile_bytes)`. Its bytes start with the TIFF
|
|
237
|
+
header (`II` or `MM`), without a JPEG APP1 marker or `Exif\0\0` prefix. ICC and
|
|
238
|
+
Exif bytes are preserved; their supported structure and field checks run before
|
|
239
|
+
PNG output. Exif thumbnails are retained as metadata, without JPEG/TIFF image
|
|
240
|
+
decoding or automatic application of historical orientation.
|
|
241
|
+
|
|
242
|
+
For a consumer that processes color descriptions, select only the formats and
|
|
243
|
+
parameter values it understands:
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
from png import PngCicp, PngImage
|
|
247
|
+
|
|
248
|
+
image = PngImage.load('created.png')
|
|
249
|
+
image.metadata.append(PngCicp(1, 13)) # BT.709 primaries, sRGB transfer function
|
|
250
|
+
selected = image.select_color_metadata([b'cICP', b'sRGB', b'gAMA', b'cHRM'])
|
|
251
|
+
print([entry.type for entry in selected]) # [b'cICP']
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Selection follows `cICP`, `iCCP`, `sRGB`, then `gAMA`/`cHRM` precedence and returns
|
|
255
|
+
independent copies. It does not transform pixel samples or tone-map HDR content.
|
|
256
|
+
PNG writers reject CICP code points reserved by the referenced H.273 (07/2024)
|
|
257
|
+
specification; `PngCicp.validate_code_points()` also exposes that check.
|
|
258
|
+
|
|
259
|
+
### Create and inspect an APNG
|
|
260
|
+
|
|
261
|
+
The default image can also be the first animation frame. Here a red canvas is
|
|
262
|
+
followed by a local blue pixel blended into its top-right corner:
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
from png import (
|
|
266
|
+
PngAnimation,
|
|
267
|
+
PngBlendOp,
|
|
268
|
+
PngDisposeOp,
|
|
269
|
+
PngFrame,
|
|
270
|
+
PngFrameControl,
|
|
271
|
+
PngHeader,
|
|
272
|
+
PngImage,
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
image = PngImage(
|
|
276
|
+
PngHeader((2, 1)),
|
|
277
|
+
bitmap=[[255, 0, 0, 255, 255, 0, 0, 255]],
|
|
278
|
+
animation=PngAnimation(
|
|
279
|
+
num_plays=0, # Repeat indefinitely
|
|
280
|
+
default_image_control=PngFrameControl((2, 1), delay_num=1, delay_den=2),
|
|
281
|
+
frames=[
|
|
282
|
+
PngFrame(
|
|
283
|
+
control=PngFrameControl(
|
|
284
|
+
dimensions=(1, 1),
|
|
285
|
+
offset=(1, 0),
|
|
286
|
+
delay_num=1,
|
|
287
|
+
delay_den=2,
|
|
288
|
+
blend_op=PngBlendOp.OVER,
|
|
289
|
+
dispose_op=PngDisposeOp.PREVIOUS,
|
|
290
|
+
),
|
|
291
|
+
bitmap=[[0, 0, 255, 128]],
|
|
292
|
+
)
|
|
293
|
+
],
|
|
294
|
+
),
|
|
295
|
+
)
|
|
296
|
+
image.save('animated.png')
|
|
297
|
+
|
|
298
|
+
loaded = PngImage.load('animated.png')
|
|
299
|
+
print(loaded.animation.num_frames) # 2
|
|
300
|
+
for number, rendered in enumerate(loaded.render_frames()):
|
|
301
|
+
print(number, rendered.delay) # Fraction(1, 2) seconds per frame
|
|
302
|
+
rendered.image.save(f'frame-{number}.png')
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
`default_image_control=None` makes `image.bitmap` a separate static fallback;
|
|
306
|
+
all animation frames then belong in `animation.frames`. Every frame uses the
|
|
307
|
+
image's color type, bit depth, and palette. Frame dimensions and offsets describe
|
|
308
|
+
a rectangle within the canvas.
|
|
309
|
+
|
|
310
|
+
`render_frames()` yields independent full-canvas RGBA snapshots for one play,
|
|
311
|
+
with exact fractional delays. It does not wait or expand `num_plays` into loops.
|
|
312
|
+
Snapshots inherit applicable color descriptions and pHYs as independent copies;
|
|
313
|
+
physical pixel dimensions do not resample the stored canvas coordinates.
|
|
314
|
+
|
|
315
|
+
### Choose interlacing, filtering, and compression
|
|
316
|
+
|
|
317
|
+
```python
|
|
318
|
+
from png import PngFilterType, PngImage, PngInterlaceMethod
|
|
319
|
+
|
|
320
|
+
image = PngImage.load('created.png')
|
|
321
|
+
image.header.interlace_method = PngInterlaceMethod.ADAM7
|
|
322
|
+
image.save(
|
|
323
|
+
'interlaced.png',
|
|
324
|
+
filter_type=PngFilterType.PAETH,
|
|
325
|
+
compression_level=9,
|
|
326
|
+
idat_chunk_size=32 * 1024,
|
|
327
|
+
)
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
The default `filter_type=None` selects filters adaptively for each scanline.
|
|
331
|
+
Compression levels range from 0 to 9; they change encoding size and cost without
|
|
332
|
+
changing pixel values. `idat_chunk_size` controls how compressed data is split
|
|
333
|
+
into IDAT/fdAT chunks. The same options work with `dump()` and `dump_to_bytes()`.
|
|
334
|
+
|
|
335
|
+
### Inspect or edit raw chunks
|
|
336
|
+
|
|
337
|
+
```python
|
|
338
|
+
from png import PngDocument, PngImage, PngText
|
|
339
|
+
|
|
340
|
+
document = PngDocument.load('created.png')
|
|
341
|
+
for chunk in document.chunks:
|
|
342
|
+
print(chunk.type, chunk.length, f'{chunk.crc:08x}', chunk.safe_to_copy)
|
|
343
|
+
|
|
344
|
+
# Add a text chunk immediately before IEND; keep existing IDAT bytes intact.
|
|
345
|
+
document.chunks.insert(-1, PngText('Comment', 'Added without pixel re-encoding').to_chunk())
|
|
346
|
+
document.save('chunk-edited.png')
|
|
347
|
+
|
|
348
|
+
# Convert between raw chunks and decoded samples explicitly.
|
|
349
|
+
image = PngImage.from_document(document)
|
|
350
|
+
reencoded_document = image.to_document()
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
`document.find_chunks(b'IDAT')` returns matching chunk objects in file order.
|
|
354
|
+
Use a metadata type's `from_chunk()` / `to_chunk()` methods for structured edits.
|
|
355
|
+
Document validation checks structure and supported metadata without decoding the
|
|
356
|
+
pixel streams; loading `PngImage` also decodes the default image and APNG frames.
|
|
357
|
+
|
|
358
|
+
Unknown ancillary chunks retain their payloads. Editing critical chunks causes
|
|
359
|
+
unknown unsafe-to-copy chunks to be omitted, and placement rules still apply.
|
|
360
|
+
Re-encoding through `PngImage` also applies those copy rules. CRCs are checked on
|
|
361
|
+
read and recomputed on write.
|
|
362
|
+
|
|
363
|
+
### Set read limits and handle errors
|
|
364
|
+
|
|
365
|
+
```python
|
|
366
|
+
from png import PngError, PngImage, PngLimitError, PngReadLimits
|
|
367
|
+
|
|
368
|
+
limits = PngReadLimits(
|
|
369
|
+
max_chunk_bytes=16 * 1024 * 1024,
|
|
370
|
+
max_total_bytes=64 * 1024 * 1024,
|
|
371
|
+
max_chunks=10000,
|
|
372
|
+
max_pixels=4_000_000,
|
|
373
|
+
max_total_pixels=16_000_000,
|
|
374
|
+
max_frames=100,
|
|
375
|
+
max_metadata_bytes=4 * 1024 * 1024,
|
|
376
|
+
)
|
|
377
|
+
try:
|
|
378
|
+
image = PngImage.load('created.png', limits=limits)
|
|
379
|
+
except PngLimitError as error:
|
|
380
|
+
print(error.limit, error.maximum, error.actual)
|
|
381
|
+
except PngError as error:
|
|
382
|
+
print(f'PNG processing failed: {error}')
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
`PngError` is the common PNG exception base; filesystem errors use ordinary
|
|
386
|
+
`OSError`. `PngLimitError` reports an application policy limit, so the input may
|
|
387
|
+
still be valid. Omitting `limits` adds no application caps. These limits count
|
|
388
|
+
data sizes, not total Python memory usage. Invalid edited models raise
|
|
389
|
+
`PngValidationError`; unsupported profile features raise `PngUnsupportedError`.
|
|
390
|
+
|
|
391
|
+
## Format and validation details
|
|
392
|
+
|
|
393
|
+
All standard PNG color types and bit depths, the five scanline filters, and
|
|
394
|
+
Adam7 are supported. APNG includes local frames, timing, default-image fallback,
|
|
395
|
+
and composition in the stored sample space.
|
|
396
|
+
|
|
397
|
+
ICC profiles expose derived `color_space` and `declared_size` fields. Readers
|
|
398
|
+
check their header and RGB/grayscale match with the PNG, preserving opaque
|
|
399
|
+
payloads for inspection. PNG file and image writers additionally check ICC
|
|
400
|
+
length/tag boundaries, selected ICC tag contents and required tags, and Exif
|
|
401
|
+
TIFF directory/value and strip/tile payload boundaries, text encodings, and selected
|
|
402
|
+
present Exif fields before writing. Errors leave the model and
|
|
403
|
+
output file unchanged. Unsupported ICC major versions, inner curve segment or
|
|
404
|
+
embedded description encodings, and Exif field types whose sizes are unsupported raise
|
|
405
|
+
`PngUnsupportedError`.
|
|
406
|
+
|
|
407
|
+
`PngIccProfile.validate_profile_structure()` and
|
|
408
|
+
`PngExif.validate_profile_structure()` expose the boundary checks explicitly.
|
|
409
|
+
`PngExif.validate_profile_tags()` adds selected present-field checks to its
|
|
410
|
+
structure checks. `PngIccProfile.validate_profile_tags()` adds header field and Profile ID checks,
|
|
411
|
+
common and class-specific required tags for ICC v2/v4, selected
|
|
412
|
+
curve, XYZ, matrix and text checks, and the `mft1`, `mft2`, `mAB` and `mBA` LUT
|
|
413
|
+
layouts and channel counts. It also checks multi-process element tables, channel
|
|
414
|
+
chains, matrices, CLUTs, segmented curves, float32 encodings, named-color records,
|
|
415
|
+
profile-sequence descriptions, identifier records and colorant tables. Colorant
|
|
416
|
+
tables require unique ASCII names with zero unused bytes; v4 `clrt`/`clot`
|
|
417
|
+
counts match the input/output color channels, and `clot` requires DeviceLink.
|
|
418
|
+
Identifier descriptions are
|
|
419
|
+
bounded by their individual records; shared records and different storage orders
|
|
420
|
+
are supported. Their IDs identify component profiles and are not compared with
|
|
421
|
+
the containing profile's ID. Unknown processing
|
|
422
|
+
elements retain their common headers and opaque payloads; a color processor must
|
|
423
|
+
use the ICC fallback rules for them. PNG writers also reject DeviceLink and
|
|
424
|
+
Abstract profiles, which cannot be embedded in images. These checks do not
|
|
425
|
+
evaluate transforms or validate every conditional requirement or full color
|
|
426
|
+
semantics. Exif checks include standard field layouts, selected closed choices,
|
|
427
|
+
text and variable-length formats, and cross-directory dependencies. Private
|
|
428
|
+
payload meanings and thumbnail pixels are not decoded.
|
|
429
|
+
Basic model validation and individual chunk serialization retain their inspection
|
|
430
|
+
role; they do not certify a whole PNG or a complete external profile.
|
|
431
|
+
|
|
432
|
+
Profile IDs may be zero (not calculated); present IDs use the ICC-defined MD5
|
|
433
|
+
calculation, including the v2 backport recommended by ICC. Validation preserves
|
|
434
|
+
the payload and does not repair IDs or change embedding flags.
|
|
435
|
+
|
|
436
|
+
V2 tag requirements follow ICC.1:2001-04 (profile version 2.4). V2 RGB display
|
|
437
|
+
profiles retain their matrix/TRC requirements when optional LUTs are present;
|
|
438
|
+
v4 also permits a LUT-only display model. Earlier v2 revisions' optional-tag
|
|
439
|
+
restrictions are not reproduced individually.
|
|
440
|
+
|
|
441
|
+
International text language tags are checked against RFC 5646 structure and
|
|
442
|
+
bundled IANA registrations, with offline snapshots dated 2026-09-17 (subtags)
|
|
443
|
+
and 2014-04-02 (extension singletons). Original case, deprecated registrations,
|
|
444
|
+
grandfathered tags, and private-use tags are preserved. Extension-specific
|
|
445
|
+
settings such as Unicode locale keys are not interpreted. See the
|
|
446
|
+
[registry sources and maintenance notes](assets/iana/README.md).
|
|
447
|
+
|
|
448
|
+
The public extensions `oFFs`, `pCAL`, `sCAL`, `gIFg`, `gIFx`, and `sTER` also
|
|
449
|
+
have structured models in `image.metadata`:
|
|
450
|
+
|
|
451
|
+
```python
|
|
452
|
+
from png import PngImage, PngOffset, PngOffsetUnit, PngPhysicalScale
|
|
453
|
+
|
|
454
|
+
image = PngImage.load('created.png')
|
|
455
|
+
image.metadata.append(PngOffset((-100, 200), PngOffsetUnit.MICROMETRE))
|
|
456
|
+
image.metadata.append(PngPhysicalScale('1.234567890123456789e-6', '1e-6'))
|
|
457
|
+
image.save('with-scale.png')
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
Calibration parameters and physical scales retain decimal strings without
|
|
461
|
+
conversion to binary floats. GIF compatibility metadata does not animate a PNG;
|
|
462
|
+
stereo metadata describes existing side-by-side pixels. These extensions follow
|
|
463
|
+
the pinned [1.6.1 editor's draft](https://github.com/w3c/png/blob/41ed1959e42c2f22706b98681b37769e00bab233/extensions/Overview.html),
|
|
464
|
+
separately from the PNG Third Edition Recommendation.
|
|
465
|
+
|
|
466
|
+
ICC v4 checks include the defined `tech` technology signatures and the `prmg`
|
|
467
|
+
reference-gamut value for `rig0`/`rig2`. Nonzero CMM, manufacturer/model, and
|
|
468
|
+
`ICCHDAT` reference names use the bundled official registry snapshot dated
|
|
469
|
+
2026-11-16. Unknown or ambiguous registrations raise `PngUnsupportedError`;
|
|
470
|
+
creator registration is advisory. See the
|
|
471
|
+
[registry provenance](assets/icc/registry/README.md).
|
|
472
|
+
|
|
473
|
+
## Development
|
|
474
|
+
|
|
475
|
+
Tests use the original [PngSuite](assets/png-suite/SOURCE.md) images and pinned
|
|
476
|
+
[WPT APNG fixtures and reference images](assets/wpt/README.md). From a checkout:
|
|
477
|
+
|
|
478
|
+
```shell
|
|
479
|
+
uv sync
|
|
480
|
+
uv run -m ruff check
|
|
481
|
+
uv run -m ruff format --check
|
|
482
|
+
uv run -m pytest
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
Passing the fixture suites is not a claim of complete
|
|
486
|
+
Third Edition or external profile certification.
|
|
487
|
+
|
|
488
|
+
## References
|
|
489
|
+
|
|
490
|
+
- [Portable Network Graphics (PNG) Specification and Extensions](https://www.libpng.org/pub/png/spec/)
|
|
491
|
+
- [Portable Network Graphics (PNG) Specification (Third Edition)](https://www.w3.org/TR/png-3/)
|
|
492
|
+
- [PngSuite](http://www.schaik.com/pngsuite/)
|
|
493
|
+
- [Web Platform Tests](https://github.com/web-platform-tests/wpt)
|
|
494
|
+
- [ICC registry](https://registry.color.org/)
|
|
495
|
+
- [Internet Assigned Numbers Authority (IANA)](https://www.iana.org/)
|
|
496
|
+
|
|
497
|
+
## License
|
|
498
|
+
|
|
499
|
+
[MIT License](LICENSE)
|