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

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

No gamma correction or scaling is performed.


Christoph Gohlke


BSD 3-Clause




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.


This release has been tested with the following requirements and dependencies (other versions may work):



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


  • Read multi-image files.

  • Fix reading ASCII formats with trailing comments.

  • Fix writing maxval=1, depth=1 binary images.

  • Use tifffile.imshow for multi-image arrays if installed.

  • Change tupltypes to bytes according to specification (breaking).


  • Allow space after token value in PAM.

  • Update metadata.


  • Add type hints.

  • Support reading PF4 RGBA FloatMaps.

  • Drop support for Python 3.7 and numpy < 1.19 (NEP29).


  • Fix unclosed file warnings.

  • Support reading PGX JPEG2000 reference images.


  • Disallow comments after last value in PNM headers.


  • Remove support for Python 3.6 (NEP 29).

  • Support os.PathLike file names.


  • Fix reading tightly packed P1 format and ASCII data with inline comments.

  • Remove support for Python 2.7 and 3.5.

  • Update copyright.


  • Move into netpbmfile package.


  • Support reading Portable FloatMaps.

  • Style fixes.


  • Use fromdata classmethod to initialize from data.

  • Support with statement.

  • Scale RGB images to maxval for display.

  • Make keyword arguments explicit.

  • Support numpy 1.10.


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

>>> 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()
(2, 2)
[[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-2023.1.1.tar.gz (29.2 kB view hashes)

Uploaded source

Built Distribution

netpbmfile-2023.1.1-py3-none-any.whl (14.4 kB view hashes)

Uploaded py3

Supported by

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