Portable gray map

From SubSurfWiki
Jump to: navigation, search

The portable gray map or PGM is a very simple raster image file format, perhaps the simplest possible. It is useful for low-level image manipulation and conversion, and for handling images with easy-to-use tools like awk and Python. PGM files can be opened in GIMP.

Format specification

A PGM file is a text file consisting of gray level values in ASCII decimal. The so-called plain format is as follows:

  • A line with the format's magic number P2, in ASCII text
  • A comment line starting with a hash character, such as # filename.pgm
  • A line with the image dimensions in columns then rows, eg 800 600 for an image 800 pixels wide
  • A line with the maximum gray level value, greater than 0 but equal to or less than 65535
  • The pixel gray values, separated by white space, with black = 0

Simple example

From Wikipedia's article about the Netpbm format, a generic superset of the PGM.

P2
# Shows the word "FEEP" (example from Netpbm main page on PGM)
24 7
15
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

Example, actual size:

Pgm example.png


...and and magnified 10×:

Pgm example 10x.png

Geoscience example

To convert a seismic timeslice to an image, make a horizon amplitude extraction on a constant time, export the horizon, extract the Z-field (e.g. with awk), change the distribution to be zero-centered (for example, for a 16-bit image, awk '{print $3+32768}' myfile.xyz > myfile.out), then add the header in a text editor.

Here are the first few lines of a PGM; the first four lines are the header:

P2
# MyHorizon.pgm
1200 800
65535
32232 
32235 
32231 
32239  
...

External links

  • PGM — Sourceforge page