PGM Image Format

The PGM (or Portable Gray 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 output a PGM image. If you are interested in more information, the formal image specification can be found here.

Sample pgm file:

	P2
	4 4
	255
	   0    33      0    170
	   0   143      0      0
	   0     0     63      0
  	 170     0      0    255
      

Image Header

The first few lines are defined as the image header. They provide an overview of the contents of the image. PGM headers consist of four enteries, which will be defined using the example:
	P2
	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 greyscale values. Each pixel of the image is a tiny, gray square. In the PGM file, each pixel is defined by a single value representing how intense, or bright, the shade of gray is. So, the first pixel, which has a value of 0, is black, and the last pixel, 255, is white. By varying the levels of the pixel values you can come up with any intensity in-between.

Note that gray values must be separated by a space, but after that any additional whitespace is ignored by the image viewer. In the sample pgm 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 gray 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 PGM 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 PGM is an intermediate format when converting images from one type to another.

How to view PGM files

On lab computers, there are many programs for viewing PGM files. ImageMagick can be invoked on the command line:

$ display image.pgm
You can also XV or GIMP. On your home computers, you may need to download an image viewer such as Irfanview.

To generate a PGM 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 > Export As.
  5. In the Export Image menu, click the arrow next to Select File Type(By Extension)
  6. Select PGM Image and then selction the name and location you would like for your image
  7. Click Export 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

  9. Note: emacs users might need to type C-c C-c to edit the PGM file as text instead of an image