Netpbm – The Portable Anymap Format

Netpbm – The Portable Anymap Format

Presenting visual data readings into an image file can seem challenging.  It need not be thanks to the Netpbm ( file formats.

First developed by Jef Poskanzer ( as a means of sending image data via email.  The early adaptations of email did not work well with data other than ASCII.  Messages would become corrupt if binary data was included in messages.  Today, this is not an issue.  However, the work around for this was to use one of the Netpbm file formats.

The Netpbm files contain plaintext ASCII information about pixels and the data they contain.  This makes it an ideal format to use when reconstructing visual type data readings.  Its simplicity removes much of the needed work used by other conventional file formats.  It also removes the burden for data processing, which is needed to create the files.  It’s a great way for the beginner to create useful data representations of data.  Lets look at the details behind this simple yet powerful file format.

As mentioned already, the Netpbm format is plain text.  I’ll be covering three distinct image types within the Netpbm suite.  These image types are defined as follows.

Type     Magic number     Magic number     Extension     Colors
Portable BitMap     P1 ASCII     P4 binary     .pbm     0–1 (black & white)
Portable GrayMap     P2 ASCII     P5 binary     .pgm     0–255 (gray scale)
Portable PixMap     P3 ASCII     P6 binary     .ppm     0–255 (RGB)

Portable BitMap images are the most simplest of all the Netpbm formats.  Each pixel is either black or white.  The file extension is PBM to depict this specific file format.  The actual data that defines the file is the header information and the beginning lines of the file.  Below is an example of a 6 x 10 pixel bitmap image.

# This is an example bitmap of the letter "J"
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

The first line is referred to as the “magic number”.  This defines the bitmap scale, P1 for black and white, P2 for gray scale, and P3 for RGB.  The next line start with a “#” symbol and is a comment line.  This is optional but can be useful to use with telemetry generating this data.  The third line defines the dimensions of the image, 6 x 10 pixels.  What follows it the meat of the file.  These are the actual pixel values to be presented.  A character return has been included so the visual effect can be seen.  However, it is more common than not that the data will be one continuous string of text.

Here are three examples of an image of Haystack Rock in the different file types.

Black and White Pixel Bitmap

Gray Scale Pixel Graymap

Color Scale Pixel Pixmap

The Gray Scale image uses the “magic number” P2.  You can see more detail in each successive image format.  The gray scale image has pixel values that range from 0 to 255, from darkest to lightest.  Opening the image file in a text editor, all of the pixels appear to be a single line item.  As mentioned before, character returns are not a requirement, only a space between the values is.

The Color Scale image uses the “magic number” P3.  Each pixel has three values defined for it in the red, green, and blue range.  Like the gray scale range, each vary from 0 to 255, from darkest to lightest.

So you might ask yourself, why not have all image formats follow this convention. The substantial reason is file size. The Netpbm file formats are extremely wasteful in regards to the data space they occupy. Binary file formats like, PNG, JPG, and GIF are more efficient. Because of this, most web content does not contain Netpbm files.

Regardless of choice of image formats on the web, Netpbm is well suited to take raw data streams and present them visually. I’ll be using the Gray Scale Pixel Graymap, also known as PGM, for my next post. In it, I’ll take temperature readings from a Melexis MLX90620 and plot a thermal image. Using the Netpbm from this will eliminate my image processing workload. I’ll have image files native to the hardware and they’ll port easily.

I look forward to seeing ahead, enjoy.

Comments are closed.