{
    "mode": "man",
    "parameter": "pgm",
    "section": "5",
    "url": "https://www.chedong.com/phpMan.php/man/pgm/5/json",
    "generated": "2026-11-14T21:37:11Z",
    "sections": {
        "NAME": {
            "content": "pgm - Netpbm grayscale image format\n\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This program is part of Netpbm(1).\n\nThe  PGM  format  is a lowest common denominator grayscale file format.  It is designed to be\nextremely easy to learn and write programs for.  (It's so simple that most people will simply\nreverse engineer it because it's easier than reading this specification).\n\nA PGM image represents a grayscale graphic image.  There are many pseudo-PGM formats  in  use\nwhere  everything  is  as specified herein except for the meaning of individual pixel values.\nFor most purposes, a PGM image can just be thought of an array of arbitrary integers, and all\nthe programs in the world that think they're processing  a  grayscale  image  can  easily  be\ntricked into processing something else.\n\nThe name \"PGM\" is an acronym derived from \"Portable Gray Map.\"\n\nOne  official variant of PGM is the transparency mask.  A transparency mask in Netpbm is rep‐\nresented by a PGM image, except that in place of pixel intensities, there are opaqueness val‐\nues.  See below.\n\n",
            "subsections": []
        },
        "THE FORMAT": {
            "content": "The format definition is as follows.  You can use the libnetpbm(1) C  subroutine  library  to\nconveniently and accurately read and interpret the format.\n\nA  PGM  file consists of a sequence of one or more PGM images. There are no data, delimiters,\nor padding before, after, or between images.\n\nEach PGM image consists of the following:\n\n\n\n\n•      A \"magic number\" for identifying the file type.  A pgm image's magic number is the two\ncharacters \"P5\".\n\n\n•      Whitespace (blanks, TABs, CRs, LFs).\n\n\n•      A width, formatted as ASCII characters in decimal.\n\n\n•      Whitespace.\n\n\n•      A height, again in ASCII decimal.\n\n\n•      Whitespace.\n\n\n•      The maximum gray value (Maxval), again in ASCII decimal.  Must be less than 65536, and\nmore than zero.\n\n\n•      A single whitespace character (usually a newline).\n\n\n•      A raster of Height rows, in order from top to bottom.  Each row consists of Width gray\nvalues, in order from left to right.  Each gray value is a number from 0 through  Max‐\nval,  with  0  being  black and Maxval being white.  Each gray value is represented in\npure binary by either 1 or 2 bytes.  If the Maxval is less than 256,  it  is  1  byte.\nOtherwise, it is 2 bytes.  The most significant byte is first.\n\nA  row  of an image is horizontal.  A column is vertical.  The pixels in the image are\nsquare and contiguous.\n\nEach gray value is a number proportional to the intensity of the  pixel,  adjusted  by\nthe  ITU-R  Recommendation  BT.709  gamma  transfer function.  (That transfer function\nspecifies a gamma number of 2.2 and has a linear section for  small  intensities).   A\nvalue  of zero is therefore black.  A value of Maxval represents CIE D65 white and the\nmost intense value in the image and any other image to which the image might  be  com‐\npared.\n\nBT.709's range of channel values (16-240) is irrelevant to PGM.\n\nNote  that  a  common variation from the PGM format is to have the gray value be \"lin‐\near,\" i.e. as specified above except without the  gamma  adjustment.   pnmgamma  takes\nsuch a PGM variant as input and produces a true PGM as output.\n\nAnother  popular  variation from PGM is to substitute the newer sRGB transfer function\nfor the BT.709 one.  You can use pnmgamma to convert between this variation  and  true\nPGM.\n\nIn  the  transparency mask variation from PGM, the value represents opaqueness.  It is\nproportional to the fraction of intensity of a pixel that would show in  place  of  an\nunderlying  pixel.   So what normally means white represents total opaqueness and what\nnormally means black represents total transparency.  In between, you would compute the\nintensity of a composite pixel of an \"under\" and \"over\" pixel as under * (1-(alpha/al‐\nphamaxval)) + over * (alpha/alphamaxval).  Note that  there  is  no  gamma  transfer\nfunction in the transparency mask.\n\n\n\nStrings starting with \"#\" may be comments, the same as with PBM(1).\n\nNote that you can use pamdepth to convert between a the format with 1 byte per gray value and\nthe one with 2 bytes per gray value.\n\nAll  characters  referred  to herein are encoded in ASCII.  \"newline\" refers to the character\nknown in ASCII as Line Feed or LF.  A \"white space\" character is space, CR, LF, TAB,  VT,  or\nFF (I.e. what the ANSI standard C isspace() function calls white space).\n\n",
            "subsections": [
                {
                    "name": "Plain PGM",
                    "content": "There  is actually another version of the PGM format that is fairly rare: \"plain\" PGM format.\nThe format above, which generally considered the normal one, is known as the \"raw\"  PGM  for‐\nmat.   See  pbm(1) for some commentary on how plain and raw formats relate to one another and\nhow to use them.\n\nThe difference in the plain format is:\n\n\n\n•\n\nThere is exactly one image in a file.\n\n•\n\nThe magic number is P2 instead of P5.\n\n•\n\nEach pixel in the raster is represented as  an  ASCII  decimal  number  (of  arbitrary\nsize).\n\n•\n\nEach  pixel in the raster has white space before and after it.  There must be at least\none character of white space between any two pixels, but there is no maximum.\n\n•\n\nNo line should be longer than 70 characters.\n\n\nHere is an example of a small image in the plain PGM format.\n\nP2\n# feep.pgm\n24 7\n15\n0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0\n0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0\n0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0\n0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0\n0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0\n0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n\n\nThere is a newline character at the end of each of these lines.\n\nPrograms that read this format should be as lenient  as  possible,  accepting  anything  that\nlooks remotely like a PGM.\n\n\n"
                }
            ]
        },
        "INTERNET MEDIA TYPE": {
            "content": "No  Internet  Media Type (aka MIME type, content type) for PGM has been registered with IANA,\nbut the value image/x-portable-graymap is conventional.\n\nNote that the PNM Internet Media Type image/x-portable-anymap also applies.\n\n\n",
            "subsections": []
        },
        "FILE NAME": {
            "content": "There are no requirements on the name of a PGM file, but the convention is to use the  suffix\n\".pgm\".   \"pnm\"  is  also conventional, for cases where distinguishing between the particular\nsubformats of PNM is not convenient.\n\n\n",
            "subsections": []
        },
        "COMPATIBILITY": {
            "content": "Before April 2000, a raw format PGM file could not have a maxval greater than 255.  Hence, it\ncould not have more than one byte per sample.  Old programs may depend on this.\n\nBefore July 2000, there could be at most one image in a PGM file.  As a result, most tools to\nprocess PGM files ignore (and don't read) any data after the first image.\n\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "pnm(1), pbm(1), ppm(1), pam(1), libnetpbm(1), programs that process PGM(1),\n\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright (C) 1989, 1991 by Jef Poskanzer.\n",
            "subsections": []
        },
        "DOCUMENT SOURCE": {
            "content": "This manual page was generated by the Netpbm tool 'makeman' from  HTML  source.   The  master\ndocumentation is at\n\nhttp://netpbm.sourceforge.net/doc/pgm.html\n\nnetpbm documentation                       09 October 2016               PGM Format Specification(5)",
            "subsections": []
        }
    },
    "summary": "pgm - Netpbm grayscale image format",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "pnm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pnm/1/json"
        },
        {
            "name": "pbm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pbm/1/json"
        },
        {
            "name": "ppm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/ppm/1/json"
        },
        {
            "name": "pam",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pam/1/json"
        },
        {
            "name": "libnetpbm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/libnetpbm/1/json"
        },
        {
            "name": "PGM",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/PGM/1/json"
        }
    ]
}