Code Monkey home page Code Monkey logo

netpbmfile's Introduction

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, 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:2024.5.24

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

Revisions

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:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.