What is PGM? Portable Graymap Format Explained
The grayscale member of the Netpbm family, storing simple uncompressed gray-level images.
Last updated:
What is PGM?
PGM, the Portable Graymap format, is the grayscale member of the Netpbm family of image formats developed by Jef Poskanzer. It extends the simple Netpbm design to store gray-level images where each pixel holds a single intensity value.
A PGM file begins with a magic number, P2 for the plain ASCII variant or P5 for the binary (raw) variant, followed by the width, height, and a maximum gray value, then the pixel data. The maximum value defines the white point and allows either 8-bit or 16-bit gray levels, with no compression applied.
How PGM Works
PGM, the Portable Graymap format, stores a single channel grayscale image. Each pixel is one integer that represents intensity, running from zero, meaning black, up to a declared maximum that means white. There is no color and no transparency, only shades of gray, which makes it the natural middle sibling between the black and white PBM and the full color PPM.
The maxval, or maximum value, is part of the header and defines the range of every pixel. A maxval of 255 gives standard 8-bit grayscale with 256 possible shades, while a larger maxval, up to 65535, allows 16-bit precision for applications that need finer tonal detail than a byte can hold. A reader uses maxval to interpret the raw numbers correctly.
A PGM header consists of a two character magic number, then the width, the height, and the maxval, all written as ASCII text separated by whitespace, followed by the pixel data laid out row by row. Because these fields are explicit, a program always knows the image size, the bit depth, and where the data begins.
Like the rest of the family, PGM comes in two variants. The plain form uses magic number P2 and writes each pixel value as readable ASCII text, while the raw form uses magic number P5 and stores the values as compact binary bytes. Both describe the same image; the raw form is simply smaller and faster to read.
History and Standardization
PGM comes from the same late 1980s toolkit created by Jef Poskanzer, originally called pbmplus and later carried forward as the Netpbm project. It was designed as a grayscale companion to the black and white PBM, using the same header conventions so that the family would feel consistent.
Together with PBM for one bit images and PPM for full color, PGM forms the trio unified under the name PNM, the Portable Anymap family. The three differ only in how much information each pixel carries, which lets tools handle them with shared logic.
Rather than being governed by a formal standards organization, PGM is defined by a short and clear specification that has remained stable for decades. That stability, plus how easy the format is to implement, is why it is trusted as a plain interchange target in image processing work.
PGM Compared to Its Siblings and Real Formats
Compared to its siblings, PGM sits between PBM and PPM. PBM can only store pure black or white, so it cannot express shading, while PPM stores three color channels per pixel and therefore uses roughly three times the data of the equivalent grayscale image. If your image is genuinely grayscale, PGM is the right sized choice among the three.
Compared to practical formats such as PNG and TIFF, PGM applies no compression, so its files are large relative to their content. PNG offers lossless compression and broad support, and TIFF offers flexible compression along with rich metadata, both of which are better for storing or distributing real grayscale images.
The honest tradeoff, as with the whole family, is simplicity against size. PGM is extremely easy to read and write, which is exactly why it is so common in image processing research and command line pipelines, where a program may generate intermediate images that are never meant to be stored long term. For final delivery of grayscale imagery, a compressed format is usually the better choice.
MKV Technical Specifications
PGM vs Other Image Formats
| Feature | PGM | PNG | BMP |
|---|---|---|---|
| Type | Raster (grayscale) | Raster | Raster |
| Color depth | Grayscale, up to 16-bit[1] | Up to 16-bit | Up to 32-bit |
| Compression | None[1] | Lossless | None or RLE |
| Transparency | No | Yes | Limited |
| Encoding | ASCII or binary[1] | Binary | Binary |
| Best for | Grayscale exchange[2] | Web graphics | Windows images |
PGM offers a minimal, uncompressed way to store grayscale images, favoring simplicity over the compression and color features of PNG or BMP.
Advantages & Disadvantages
Advantages
PGM cleanly represents gray-level images with a minimal, easy-to-parse structure.
The P2 ASCII form is human-readable, while the P5 raw form is more compact for the same image.
The maximum gray value field supports both 8-bit and 16-bit precision per pixel.
It is widely handled by image-processing toolkits and is easy to generate programmatically.
Disadvantages
PGM cannot store color; color images require the related PPM format.
Uncompressed storage makes files larger than compressed grayscale formats such as PNG.
The format carries only essential image dimensions and a max value, with no rich metadata support.
Common Use Cases
PGM is a convenient intermediate format for grayscale image processing.
Computer vision input
Many image-processing and vision tools accept PGM grayscale images for analysis.
Netpbm conversions
PGM serves as an intermediate when converting grayscale images between formats.
Scientific and depth data
Single-channel intensity or height data can be stored straightforwardly as PGM.
Convert PGM Files Free
Use our free online image converter to convert PGM and other formats - no signup, no watermarks.
Try Image Converter FreeHow to open a PGM file
Software for working with PGM files can be categorized into professional tools, free/open-source applications, browser-based solutions, and built-in utilities. Professional tools are suited for users needing advanced features and high fidelity, while free/open-source options cater to those who prioritize cost-effectiveness and community support. Browser-based solutions offer convenience for quick tasks without installation, and built-in utilities provide basic functionality for casual users.
When choosing software for PGM files, factors such as feature fidelity, whether the focus is on editing or viewing, cost model, and platform compatibility are crucial. Users should consider their export needs, as some applications provide robust conversion options while others may be limited. In some cases, converting PGM files to more widely supported formats may be a better move for compatibility and ease of use.
GIMP 2.10.34
GIMP is a powerful open-source image editor that supports PGM files for both viewing and editing. It allows users to manipulate grayscale images with high fidelity, making it suitable for detailed graphic work.
- Advanced layer and mask editing
- Customizable brushes and filters
- Support for high bit-depth images
- Batch processing capabilities
- Extensive plugin support
- Highly customizable interface and tools
- Robust community and support resources
- No cost for full feature access
- Steeper learning curve for beginners
- Performance can lag on large files
Exports to: PNG, JPG, TIFF, BMP, GIF
Best for: GIMP is ideal for users needing a comprehensive editing suite for PGM files without the cost.
Visit GIMP →ImageMagick 7.1.0-16
ImageMagick is a command-line tool that excels in converting and manipulating images, including PGM files. It is particularly useful for batch processing and automation of image tasks.
- Command-line interface for automation
- Batch conversion of multiple files
- Extensive format support and manipulation
- Image resizing and filtering options
- Support for scripting and integration
- Powerful for batch processing tasks
- Lightweight and fast performance
- Highly versatile with scripting capabilities
- No graphical user interface
- Requires command-line knowledge
Exports to: PNG, JPG, TIFF, GIF, BMP
Best for: ImageMagick is perfect for advanced users who need powerful batch processing and automation for PGM files.
Visit ImageMagick →IrfanView 4.60
IrfanView is a lightweight image viewer that supports a variety of formats, including PGM. It is designed for quick viewing and basic editing, making it user-friendly for casual users.
- Fast image viewing and browsing
- Basic editing tools like cropping and resizing
- Slideshow creation capabilities
- Support for plugins to extend functionality
- Batch conversion and renaming features
- Extremely fast and lightweight
- User-friendly interface for beginners
- Good support for batch processing
- Limited advanced editing features
- Windows-only platform
Exports to: PNG, JPG, BMP, TIFF, GIF
Best for: IrfanView is suitable for users who need a quick and easy way to view and perform basic edits on PGM files.
Visit IrfanView →Photoshop 2023
Adobe Photoshop is a leading professional image editing software that supports PGM files. It offers extensive editing capabilities, making it suitable for high-quality image manipulation and design work.
- Comprehensive layer and mask editing
- Advanced color correction tools
- Support for 16-bit and 32-bit images
- Extensive filter and effect options
- Integration with Adobe Creative Cloud
- Industry-standard editing tools
- Rich feature set for professional use
- Seamless integration with other Adobe apps
- Subscription model can be costly
- Complexity may overwhelm new users
Exports to: PNG, JPG, TIFF, PSD, PDF
Best for: Photoshop is ideal for professionals who require advanced editing features and high fidelity for PGM files.
Visit Photoshop →