What is the PGM file format?
.pgm (short for Portable Gray Map) is a file format associated with Netpbm, a cross-platform bitmap graphics toolkit.
What do PGM files contain?
PGM files store 8- or 16-bit grayscale images. A single .pgm file can store multiple such images. Each pixel can take a value from 0 (black) to 65 535 (white).
How is data saved in PGM files?
PGM files can save data in the following variants:
- plain text (file header with magic number
P2), human-readable ASCII encoding - binary (file header with magic number
P5), storing raw binary samples per pixel
Both variants support multiple concatenated images in a single file. The file header defines width, height, and the maximum gray value (Maxval). The body of the file contains sample values for each image pixel.
What are PGM files used for?
A .pgm file is an example of the .pnm (Portable Anymap) standard implementation. PGM files are chiefly used as an image interchange format in image processing, computer vision pipelines, and graphics toolchains. Besides PGM, other implementations in the same family are available:
Security & safety
RISK: LOWPGM is a passive, fully-documented raster image - header plus pixel data, no scripts or executable content, so opening one is safe. The only practical cautions are non-malware: files are large because PGM is uncompressed (convert to PNG to save space), and a malformed or oversized header could make a naive parser allocate a huge buffer, so use a maintained viewer. There is no metadata/EXIF privacy concern as the format carries none.
Format details
in a nutshellPrograms that open PGM files
Technical details
deep spec| Format family | Netpbm PNM family, alongside .pbm (1-bit bitmap) and .ppm (RGB color) |
| Color model | Grayscale only - no color channels, no alpha channel |
| Color depth | 8-bit (Maxval 255, 256 shades of gray) or 16-bit (Maxval up to 65 535) |
| Magic number | "P2" (0x50 0x32) for the ASCII variant; "P5" (0x50 0x35) for the binary variant - located at offset 0 |
| MIME type | image/x-portable-graymap |
| Encoding variants | P2: human-readable plain ASCII pixel values; P5: raw binary pixel data - the header is always ASCII text in both variants |
| Compression | None - pixel samples are stored raw and uncompressed |
| Byte order | Big-endian for 16-bit samples (when Maxval > 255); 8-bit samples occupy a single byte each |
| Bytes per sample | 1 byte per pixel when Maxval ≤ 255; 2 big-endian bytes per pixel when Maxval > 255 |
| Header structure | Whitespace-separated ASCII fields in order: magic number, image width, image height, Maxval - followed by a single whitespace delimiter, then the pixel raster |
| Multi-image support | Multiple images may be concatenated sequentially within one .pgm file (both P2 and P5 variants) |
| Dimensions limit | No format-imposed maximum; bounded only by the ASCII integer range in the header and available memory |
| Typical file size | Uncompressed; scales as width × height × bytes-per-sample - ranging from kilobytes for small images to tens of megabytes for large ones |
| Developer | Jef Poskanzer, originating from the Pbmplus toolkit (later continued as the Netpbm project) |
| Platform support | Windows, macOS, Linux, Unix - natively handled by Netpbm tools and supported by most image processing libraries (OpenCV, ImageMagick, GIMP) |
| Released | 1988 (Pbmplus toolkit, later Netpbm) |
| Latest version | Stable since the 1990s; current Netpbm 10.86+ implements it (no format version bump) |
| Open standard | Yes · royalty-free |
| Specification | netpbm.sourceforge.net |
PGM conversions
Community Q&A
asked by usersNo questions yet - be the first to ask about PGM files.