PPM Image Format

 

The PPM (or Portable Pix Map) image format is encoded in human-readable text. Below is a brief synopsis of the format which will aid you in being able to load a PPM image. If you are interested in more information, the formal image specification can be found here.

Sample ppm file:

	P3
	4 4
	255
	0  0  0   100 0  0       0  0  0    255   0 255
	0  0  0    0 255 175     0  0  0     0    0  0
	0  0  0    0  0  0       0 15 175    0    0  0
	255 0 255  0  0  0       0  0  0    255  255 255
      

Image Header

The first few lines are defined as the image header. They provide an overview of the contents of the image. PPM headers consist of four enteries, which will be defined using the example:
	P3
	4 4
	255
			
The way you see the header presented is how it should be spaced out.

Image Body

The image body contains the actual picture information as a series of RGB values. Each pixel of the image is a tiny, colored square. In the PPM file, each pixel is defined by a triplet of values representing how much red, green, and blue (RGB) are present. So, the first pixel, which has a value of 0 0 0, is black, and the last pixel, 255 255 255, is white. r By varying the levels of the RGB values you can come up with any color in between.

Note that color values must be separated by a space, but after that any additional whitespace is ignored by the image viewer. In the sample ppm above we used additional whitespace to format the pixel values so that it is easy for a human to understand, but the computer doesn't care if everything is on one line, if there is one line of RGB values per line of the image, or some mix. You should not assume one line in the file corresponds to one line in the image.

The example image above would look something like this:

Keep in mind, each square is one pixel, so the real thing is much smaller (the rendered image was blown up by 5000%).

As an aside, while PPM files are easy to view as text (you can use vim, emacs, or Notepad, for instance), they are highly inefficient. Most modern image formats use some form of compression to reduce the amount of information stored (and consequently, their size) while preserving the image appearance. One modern use for PPM is an intermediate format when converting images from one type to another.

How to view PPM files

On lab computers, there are many programs for viewing PPM files. If you are in a directory containing one or more images, the geeqie program can view all the images in the directory

$ geeqie &
On your home computers, you may need to download an image viewer such as Irfanview.

To generate a PPM image from an image in your photo library, run the following command to convert an image.

$ convert my_image.jpg -compress none my_image.ppm
Replace my_image.jpg and my_image.ppm with your desired input and output filenames, respectively.