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. ImageMagick can be invoked on the command line:

$ display image.ppm
You can also XV or GIMP. 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, do the following in GIMP:

  1. Open the file in GIMP
  2. $ gimp image.jpg
    
  3. Scale the image down to a manageable size (at most no dimension should be larger than 1000 pixels). Do this by selecting from the Menu: Image > Scale Image. Then, select the width to be some value smaller than 1000. By default, the height will scale proportionally. Click Scale
  4. Select File > Save Image As.
  5. In the Save Image menu, click the arrow next to Select File Type(By Extension)
  6. Select PPM Image and then selction the name and location you would like for your image
  7. Click Save and then select Ascii for data format
  8. Open the file with your favorite editor and remove the commented line
    # CREATOR: GIMP PNM Filter Version 1.1