Skip to main content

Read and write Netpbm files

Netpbmfile is a Python library to read and write image files in the Netpbm or related formats:

  • PBM (Portable Bit Map): P1 (text) and P4 (binary)

  • PGM (Portable Gray Map): P2 (text) and P5 (binary)

  • PPM (Portable Pixel Map): P3 (text) and P6 (binary)

  • PNM (Portable Any Map): shorthand for PBM, PGM, and PPM collectively

  • PAM (Portable Arbitrary Map): P7, bilevel, gray, rgb, and arbitrary depths

  • PGX (Portable Graymap Signed): PG, signed grayscale

  • PFM (Portable Float Map): Pf (gray), PF (rgb), and PF4 (rgba), read-only

  • XV thumbnail: P7 332 (rgb332), read-only

The Netpbm formats are specified at https://netpbm.sourceforge.net/doc/.

The PGX format is specified in ITU-T Rec. T.803.

No gamma correction or scaling is performed.

Author:

Christoph Gohlke

License:

BSD-3-Clause

Version:

2026.10.15

DOI:

10.5281/zenodo.17903402

Quickstart

Install the netpbmfile package and all dependencies from the Python Package Index:

python -m pip install -U "netpbmfile[all]"

See Examples and Documentation for using the programming interface.

Source code and support are available on GitHub.

Requirements

This revision was tested with the following requirements and dependencies (other versions may work):

  • CPython 3.12.10, 3.13.16, 3.14.8, 3.15.0 64-bit

  • Numpy 2.5.4

Revisions

2026.10.15

  • Fix silent truncation of samples not fitting dtype of maxval.

  • Write to open files at current position (breaking).

  • Do not close caller-owned file handle on parsing error.

  • Raise when writing XV format instead of writing PAM (breaking).

2026.7.25

  • Drop support for Python 3.11 and numpy 2.0 (SPEC0), support Python 3.15.

2026.1.29

  • …

Refer to the CHANGES file for older revisions.

Examples

Import functions and classes used in these examples:

>>> from netpbmfile import NetpbmFile, imread, imwrite

Write a numpy array to a Netpbm file in grayscale binary format:

>>> import numpy
>>> data = numpy.array([[0, 1], [65534, 65535]], dtype=numpy.uint16)
>>> imwrite('_tmp.pgm', data)

Read the image data from a Netpbm file as numpy array:

>>> image = imread('_tmp.pgm')
>>> numpy.testing.assert_equal(image, data)

Access meta and image data in a Netpbm file:

>>> with NetpbmFile('_tmp.pgm') as pgm:
...     pgm.magicnumber
...     pgm.axes
...     pgm.shape
...     pgm.dtype
...     pgm.maxval
...     pgm.asarray().tolist()
...
'P5'
'YX'
(2, 2)
dtype('>u2')
65535
[[0, 1], [65534, 65535]]

View the image and metadata in the Netpbm file from the command line:

$ python -m netpbmfile _tmp.pgm

Metadata

Release files for netpbmfile 2026.10.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for netpbmfile 2026.10.15
File Size Uploaded
netpbmfile-2026.10.15.tar.gz 32.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for netpbmfile 2026.10.15
File Interpreter ABI Platform
netpbmfile-2026.10.15-py3-none-any.whl Python 3 none any Details

Total release size: 46.7 kB

Release files / netpbmfile-2026.10.15.tar.gz

Download URL netpbmfile-2026.10.15.tar.gz
Size 32.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fd8b646c34aeccfe7f7dc221843790a558fa2c5ede6d8d8df795010b14f4c4ba
BLAKE2b-256 checksum
How to use checksums
6ce6a5886e471274cbe7b2e1d51148f1bb540cfe35adea399c8fc960db744dac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / netpbmfile-2026.10.15-py3-none-any.whl

Download URL netpbmfile-2026.10.15-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3bd697bea50fe332ab440ac0c87f9366f3be32dd31984966751e466d990b233
BLAKE2b-256 checksum
How to use checksums
460b1414101de0d5bb3a72f3cf9a89689555da1ff61e0781269e639e449c04db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page