clj-pnm

A small Clojure(Script) library for reading and writing Netpbm format. Supports plain text formats: p1 (.pbm), p2 (.pgm) and p3 (.ppm).

Good explanation of the format can be found here and here.

Some code samples:

;; read the pbm format
(require '[clj-pnm.core :as pnm])
=> nil

(def pbm
"P1
 2
 2
 1 1
 1 1")
=> #'user/pbm

(pnm/read-pnm pbm)
=> {:type :p1, :width 2, :height 2, :map [[1 1] [1 1]]}

;; read the *pgm* format
(def pgm
"P2
 3
 2
 4 # this is the maximum value
 1 2 3
 4 3 2")
=> #'user/pgm

(pnm/read-pnm pgm)
=> {:type :p2,
    :width 3,
    :height 2,
    :max-value 4,
    :map [[1 2 3] [4 3 2]]
    :comments ("4  this is the maximum value")} ; prettified

;; read the *ppm* format
(def ppm
"P3
 2
 2
 255
 # A ppm comment
 255 0 255 128 52 123
 0 0 0 45 45 45")
=> #'user/ppm

(pnm/read-pnm ppm)
=> {:type :p3
    :width 2
    :height 2
    :max-value 255
    :map [[(255 0 255) (128 52 123)]
          [(0 0 0) (45 45 45)]]
    :comments ("A ppm comment")} ; prettified

The Clojure version can write Netpbm format to a file:

(pnm/write-pnm {:type :p1 :width 1 :height 1 :map [[1]]} "out.pbm")
=> ...out.pbm file...

(slurp *1)
=> "P1
 1
 1
 1" ;prettified

[Edited 20.06.2023.]

clj-pnm is now available on Clojars.

For more info and more examples check out the repo.