.PGM

PGM File

Portable Graymap (Netpbm grayscale image)
Ask a question
QUICK ANSWER

A PGM file is a Portable Graymap - a simple, uncompressed grayscale image from the Netpbm family, created by Jef Poskanzer in 1988. Open it free with GIMP, IrfanView or XnView on Windows; Preview or GIMP on macOS; GIMP or Netpbm on Linux. To share it, convert to PNG (lossless) or JPG.

Developer: Jef Poskanzer (Pbmplus / Netpbm project) Category: Raster Image Files Open standard MIME: image/x-portable-graymap
OPENS ON Windows macOS Linux
Related: .JPG · .PNG · .TIF · .ECW

On this page

19k+ extensions indexed
Last reviewed Aug 26, 2026

Not sure what your file is?

Drop any file into our identifier - we read just the first bytes to name the format.

Identify a file

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:

  • .pbm for storing black-and-white (1-bit) images
  • .ppm for storing full-color RGB images

Security & safety

RISK: LOW

PGM 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 nutshell
FULL NAMEPortable Graymap (Netpbm grayscale image)aka Portable Gray Map, Netpbm PGM
DEVELOPERJef Poskanzer (Pbmplus / Netpbm project)since 1988 (Pbmplus toolkit, later Netpbm)
MIME TYPEimage/x-portable-graymap
TYPEUncompressed Netpbm raster grayscale bitmap; ASCII (P2) or raw binary (P5) variant
STANDARDOpen · royalty-free
MAGIC BYTES · FILE SIGNATURE
OFFSET
0001
HEX
5032
ASCII
P2
The file begins with a two-byte magic number: "P2" (50 32) for the human-readable ASCII variant or "P5" (50 35) for the raw binary variant. After it come whitespace-separated ASCII fields - width, height and the maximum gray value (Maxval, e.g. 255) - then the pixel data. Siblings: P1/P4 = PBM (bitmap), P3/P6 = PPM (color RGB).

Programs that open PGM files

Windows4 apps
GIMP Open-source File > Open the .pgm directly; GIMP reads P2 and P5, then export to PNG/JPG via File > Export As.
IrfanView Free Open the .pgm to view instantly; use File > Save As or batch-convert to PNG/JPG.
ImageMagick Open-source Command line: 'magick input.pgm output.png' to convert; 'magick identify input.pgm' to inspect.
XnView MP Free Open or browse PGM files; XnConvert (same maker) batch-converts folders of PGM to PNG/JPG.
macOS3 apps
GIMP Open-source Open the .pgm and export to a standard format; reads both PGM variants.
Preview Built-in macOS Preview opens many PGM files directly by double-clicking; export via File > Export.
ImageMagick Open-source Install via Homebrew, then 'magick in.pgm out.png' to convert/view.
Linux3 apps
GIMP Open-source Open the .pgm directly; export to PNG/JPG.
Netpbm Open-source The reference toolkit: 'pnmtopng', 'pgmtoppm', 'pnmtojpeg' convert PGM to other formats; 'display' shows it.
ImageMagick / GraphicsMagick Open-source 'display in.pgm' to view; 'convert in.pgm out.png' to transcode.

Technical details

deep spec
Format familyNetpbm PNM family, alongside .pbm (1-bit bitmap) and .ppm (RGB color)
Color modelGrayscale only - no color channels, no alpha channel
Color depth8-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 typeimage/x-portable-graymap
Encoding variantsP2: human-readable plain ASCII pixel values; P5: raw binary pixel data - the header is always ASCII text in both variants
CompressionNone - pixel samples are stored raw and uncompressed
Byte orderBig-endian for 16-bit samples (when Maxval > 255); 8-bit samples occupy a single byte each
Bytes per sample1 byte per pixel when Maxval ≤ 255; 2 big-endian bytes per pixel when Maxval > 255
Header structureWhitespace-separated ASCII fields in order: magic number, image width, image height, Maxval - followed by a single whitespace delimiter, then the pixel raster
Multi-image supportMultiple images may be concatenated sequentially within one .pgm file (both P2 and P5 variants)
Dimensions limitNo format-imposed maximum; bounded only by the ASCII integer range in the header and available memory
Typical file sizeUncompressed; scales as width × height × bytes-per-sample - ranging from kilobytes for small images to tens of megabytes for large ones
DeveloperJef Poskanzer, originating from the Pbmplus toolkit (later continued as the Netpbm project)
Platform supportWindows, macOS, Linux, Unix - natively handled by Netpbm tools and supported by most image processing libraries (OpenCV, ImageMagick, GIMP)
Released1988 (Pbmplus toolkit, later Netpbm)
Latest versionStable since the 1990s; current Netpbm 10.86+ implements it (no format version bump)
Open standardYes · royalty-free
Specificationnetpbm.sourceforge.net

PGM conversions

Community Q&A

asked by users
Ask a quick question
Get help from people who work with PGM files. Be specific - include your system and software version.
No account needed · answers usually within a day

No questions yet - be the first to ask about PGM files.

Frequently asked questions

What program opens a PGM file?
The free GIMP, IrfanView and XnView all open PGM (both the P2 ASCII and P5 binary variants) on Windows; GIMP and Preview work on macOS; GIMP, Netpbm and ImageMagick on Linux.
How do I convert a PGM to PNG or JPG?
Open it in IrfanView, XnConvert, GIMP or ImageMagick and Save As / Export to PNG or JPG. For many files at once, IrfanView's and XnConvert's batch mode, or ImageMagick's 'mogrify -format png *.pgm', convert a whole folder.
What's the difference between PGM, PPM and PBM?
They're the same Netpbm family: PBM is 1-bit black-and-white, PGM is grayscale, and PPM is full RGB color. PNM/PAM are umbrella names covering all three.
What's the difference between P2 and P5 PGM?
P2 stores pixels as human-readable ASCII numbers (you can open it in a text editor) - larger but inspectable. P5 stores the same pixels as raw bytes - smaller and faster. Both are valid PGM.
Why is my PGM file so large?
PGM uses no compression at all - it stores every pixel in full. Converting to PNG (lossless) or JPG (lossy) shrinks it dramatically with no or little quality change.
Can I open a PGM in a text editor?
Only the P2 (ASCII) variant meaningfully - you'll see the header and gray-value numbers, not an image. P5 (binary) PGMs are raw bytes and look like gibberish in a text editor; use an image viewer instead.

References

1Netpbm - PGM format specificationnetpbm.sourceforge.net
2Netpbm project homenetpbm.sourceforge.net

Keep exploring

across the database

Top extensions this week

1.AQQAQQ Instant Messenger File
2.CRDOWNLOADChrome Partial Download File
3.PARTPartial Download File
4.EXEWindows Executable (Portable Executable)
5.DATProgram Data File (generic)
6.MDMarkdown Document
7.TMPTemporary File
8.BINCD/DVD Disc Image (BIN/CUE)
9.M4AMPEG-4 Audio File
10.ODTOpenDocument Text

Related extensions

.JPGJPEG Image
.PNGPortable Network Graphics
.TIFTagged Image File Format
.ECWEnhanced Compression Wavelet (ECW) image
.AVIFAV1 Image File Format (AVIF)
.JP2JPEG 2000 Image (ISO/IEC 15444-1)

Free file tools

An in-browser file identifier and image converter - everything runs on your device.

Open the toolbox

Browse file extensions A-Z