Skip to main content

Read and write Netpbm files

Project description

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, and rgb

  • 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:

2025.5.8

Quickstart

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

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

See Examples 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.10.11, 3.11.9, 3.12.9, 3.13.2 64-bit

  • NumPy 2.2.5

Revisions

2025.5.8

  • Remove doctest command line option.

2025.1.1

  • Improve type hints.

  • Drop support for Python 3.9, support Python 3.13.

2024.5.24

  • Fix docstring examples not correctly rendered on GitHub.

2024.4.24

  • Support NumPy 2.

2023.8.30

  • Fix linting issues.

  • Add py.typed marker.

2023.6.15

  • Drop support for Python 3.8 and numpy < 1.21 (NEP29).

  • Improve type hints.

2023.1.1

  • Several breaking changes:

  • Rename magicnum to magicnumber (breaking).

  • Rename tupltypes to tupltype (breaking).

  • Change magicnumber and header properties to str (breaking).

  • Replace pam parameter with magicnumber (breaking).

  • Move byteorder parameter from NetpbmFile.asarray to NetpbmFile (breaking).

  • Fix shape and axes properties for multi-image files.

  • Add maxval and tupltype parameters to NetpbmFile.fromdata and imwrite.

  • Add option to write comment to PNM and PAM files.

  • Support writing PGX and text formats.

  • Add Google style docstrings.

  • Add unittests.

2022.10.25

Refer to the CHANGES file for older revisions.

Examples

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netpbmfile-2025.5.8.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

netpbmfile-2025.5.8-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file netpbmfile-2025.5.8.tar.gz.

File metadata

  • Download URL: netpbmfile-2025.5.8.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for netpbmfile-2025.5.8.tar.gz
Algorithm Hash digest
SHA256 98f0c9f07e073639f00b240e29184e4702d37243bb301d49f07202f5dbb08b7e
MD5 39c0a15fa3d6f2549f91337713fece50
BLAKE2b-256 0d6c3ffb73952de5db2c85ca8a7745fd0252b9df4f0652214cf54240a505ea25

See more details on using hashes here.

File details

Details for the file netpbmfile-2025.5.8-py3-none-any.whl.

File metadata

  • Download URL: netpbmfile-2025.5.8-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for netpbmfile-2025.5.8-py3-none-any.whl
Algorithm Hash digest
SHA256 cbd8643ecc056d52db8c519967d6dc8942c72d9f2d07d65bbb4a6f270deb6c9f
MD5 432159609f8a403c20a081316384968b
BLAKE2b-256 a32b8d960c90f7e97225a23350fe56042f57bd86995cc4686910e4b2ae1d291a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page