PGM(5)

๐Ÿ’– PGM โ€” the manual page, but make it make sense ๐Ÿ’–

okay so bestie, you're reading the PGM Format Specification, and it lives in section 5 of the manual (that's the File Formats Manual, for the record). it's giving "official documentation" but honestly it's giving "my notes app at 2am," and I mean that as the highest compliment. ๐Ÿ’…

โœจ NAME โœจ

pgm โ€” the Netpbm grayscale image format. that's it, that's the name. short, iconic, no notes. ๐ŸŽ€

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so this whole thing is part of Netpbm(1). ๐Ÿซถ

the PGM format is a lowest common denominator grayscale file format. it was literally designed to be extremely easy to learn and write programs for. and when I say easy? it's so simple that most people just reverse engineer it instead of reading this spec, because reverse engineering is genuinely easier than reading the spec. no cap. ๐Ÿ˜ญ

a PGM image represents a grayscale graphic image. here's the tea though: there are a LOT of pseudo-PGM formats in use where everything is exactly as specified here except for the meaning of the individual pixel values. for most purposes you can just think of a PGM image as an array of arbitrary integers, and every program in the world that thinks it's processing a grayscale image can easily be tricked into processing something else. gaslight, gatekeep, gray-scale. ๐Ÿ’€

the name "PGM" is an acronym derived from "Portable Gray Map." cute, right? โœจ

one official variant of PGM is the transparency mask. a transparency mask in Netpbm is represented by a PGM image, except that in place of pixel intensities, there are opaqueness values. more on that below, keep scrolling bestie. ๐Ÿ‘‘


๐Ÿฆ‹ THE FORMAT ๐Ÿฆ‹

here's the actual format definition, fr fr. and hey โ€” you can use the libnetpbm(1) C subroutine library to conveniently and accurately read and interpret the format, so you don't have to rawdog it. ๐Ÿ’œ

a PGM file consists of a sequence of one or more PGM images. there is no data, delimiters, or padding before, after, or between images. clean. minimal. aesthetic. ๐ŸŒˆ

each PGM image consists of the following:

  • A "magic number" for identifying the file type. a pgm image's magic number is the two characters "P5".
  • Whitespace (blanks, TABs, CRs, LFs).
  • A width, formatted as ASCII characters in decimal.
  • Whitespace.
  • A height, again in ASCII decimal.
  • Whitespace.
  • The maximum gray value (Maxval), again in ASCII decimal. must be less than 65536, and more than zero.
  • A single whitespace character (usually a newline).
  • A raster of Height rows, in order from top to bottom. each row consists of Width gray values, in order from left to right. each gray value is a number from 0 through Maxval, with 0 being black and Maxval being white. each gray value is represented in pure binary by either 1 or 2 bytes. if the Maxval is less than 256, it is 1 byte. otherwise, it is 2 bytes. the most significant byte is first.

a row of an image is horizontal. a column is vertical. the pixels in the image are square and contiguous. ๐ŸŸช๐ŸŸช๐ŸŸช

each gray value is a number proportional to the intensity of the pixel, adjusted by the ITU-R Recommendation BT.709 gamma transfer function. (that transfer function specifies a gamma number of 2.2 and has a linear section for small intensities.) a value of zero is therefore black. a value of Maxval represents CIE D65 white and the most intense value in the image and any other image to which the image might be compared.

BT.709's range of channel values (16-240) is irrelevant to PGM. don't even worry about it. ๐Ÿ™ˆ

note that a common variation from the PGM format is to have the gray value be "linear," i.e. as specified above except without the gamma adjustment. pnmgamma takes such a PGM variant as input and produces a true PGM as output. king behavior. ๐Ÿ‘‘

another popular variation from PGM is to substitute the newer sRGB transfer function for the BT.709 one. you can use pnmgamma to convert between this variation and true PGM.

in the transparency mask variation from PGM, the value represents opaqueness. it is proportional to the fraction of intensity of a pixel that would show in place of an underlying pixel. so what normally means white represents total opaqueness and what normally means black represents total transparency. in between, you would compute the intensity of a composite pixel of an "under" and "over" pixel as:

under * (1-(alpha/alpha_maxval)) + over * (alpha/alpha_maxval)

note that there is no gamma transfer function in the transparency mask. periodt. ๐Ÿ’…

strings starting with "#" may be comments, the same as with PBM(1).

note that you can use pamdepth to convert between the format with 1 byte per gray value and the one with 2 bytes per gray value. glow up, bestie. โœจ

all characters referred to herein are encoded in ASCII. "newline" refers to the character known in ASCII as Line Feed or LF. a "white space" character is space, CR, LF, TAB, VT, or FF (i.e. what the ANSI standard C isspace() function calls white space).

๐ŸŽ€ Minimal Subset ๐ŸŽ€

the PGM format is SO simple that many programmers write programs to process it directly, without using a library such as libnetpbm, reverse engineering a subset of the format from examples rather than using the specification. so if you want to produce an image that as many programs as possible can read, you should stick to the following subset. it's the "everyone can sit with us" version. ๐Ÿซถ

the image consists of the following bytes:

  • 'P5' followed by newline
  • width and height separated by a space (e.g. '50 100'), followed by newline
  • maxval, followed by newline. at most 255; even more minimally, 255 exactly.
  • The raster

there is only one image in the file. no sequels, no spinoffs.

the program pnmtopnm produces this minimal subset, so if you have a more complex PGM image, you can pass it to a less tolerant program through pnmtopnm. pamdepth can make the maxval 255. ๐Ÿ’–

๐Ÿงš Plain PGM ๐Ÿงš

there is actually another version of the PGM format that is fairly rare: "plain" PGM format. the format above, which is generally considered the normal one, is known as the "raw" PGM format. see pbm(1) for some commentary on how plain and raw formats relate to one another and how to use them.

the difference in the plain format is:

  • There is exactly one image in a file.
  • The magic number is P2 instead of P5.
  • Each pixel in the raster is represented as an ASCII decimal number (of arbitrary size).
  • Each pixel in the raster has white space before and after it. there must be at least one character of white space between any two pixels, but there is no maximum.
  • No line should be longer than 70 characters.

here is an example of a small image in the plain PGM format. look at her. she's beautiful. ๐Ÿฆ‹

P2
# feep.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

there is a newline character at the end of each of these lines. do not forget the newline, I am begging you. ๐Ÿ˜ญ

programs that read this format should be as lenient as possible, accepting anything that looks remotely like a PGM image. be kind, be forgiving, be that girl. ๐Ÿ’œ


๐ŸŒˆ INTERNET MEDIA TYPE ๐ŸŒˆ

no Internet Media Type (aka MIME type, content type) for PGM has been registered with IANA, but the value image/x-portable-graymap is conventional.

note that the PNM Internet Media Type image/x-portable-anymap also applies. so you've got options, bestie. โœจ

๐Ÿ“ FILE NAME ๐Ÿ“

there are no requirements on the name of a PGM file, but the convention is to use the suffix .pgm. pnm is also conventional, for cases where distinguishing between the particular subformats of PNM is not convenient.

๐Ÿ‘‘ COMPATIBILITY ๐Ÿ‘‘

  • Before April 2000, a raw format PGM file could not have a maxval greater than 255. hence, it could not have more than one byte per sample. old programs may depend on this. โš ๏ธ
  • Before July 2000, there could be at most one image in a PGM file. as a result, most tools to process PGM files ignore (and don't read) any data after the first image.
okay so real talk: if you're feeding old software, keep those two caveats in your head. they will absolutely humble you. ๐Ÿ’€

๐Ÿ’… SEE ALSO ๐Ÿ’…

pnm(1), pbm(1), ppm(1), pam(1), libnetpbm(1), programs that process PGM(1)

๐Ÿ’œ AUTHOR ๐Ÿ’œ

Copyright (C) 1989, 1991 by Jef Poskanzer.

๐ŸŽ€ DOCUMENT SOURCE ๐ŸŽ€

this manual page was generated by the Netpbm tool makeman from HTML source. the master documentation is at

http://netpbm.sourceforge.net/doc/pgm.html

and that's the whole page, bestie!! ๐Ÿซถ this was netpbm documentation, dated 07 November 2025, and you just read the PGM Format Specification(5). go forth and write the simplest image parser of your life. you ate. ๐Ÿ’–โœจ๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from