This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Mathematica Import/Export Format

PGM (.pgm)

MIME type: image/x-portable-graymap
PGM grayscale raster image format.
Member of the Portable family of image formats.
Related to PBM and PPM.
Implements a subset of the PNM format.
Native format of the Netpbm graphics software package.
Primarily used as an intermediate file format on Unix systems.
PGM is an acronym for Portable Gray Map.
ASCII or binary format.
Stores raster images formatted as plain text, representing each pixel as an 8-bit gray level.
Developed in 1988 as part of the Pbmplus package.
  • Export creates binary PGM files.
  • If multiple images are present in a PGM file, Import returns the first of the images.
  • Import["file.pgm"] imports a PGM file, returning a graphics object.
  • Export["file.pgm", expr] exports graphics or any other expression as a PGM raster image.
  • Export by default generates binary PGM files.
  • Import["file.pgm"] returns an expression of the form Graphics[Raster[data], opts], where data is an array of gray-level values.
  • Export["file.pgm", expr] creates a PGM image with the same image size as the displayed form of expr.
  • Because PGM is a raster image format, all fonts are rasterized on export.
  • Import["file.pgm", elem] imports the specified element from a PGM file.
  • Import["file.pgm", {elem, suba, subb, ...}] imports a subelement.
  • Import["file.pgm", {{elem1, elem2, ...}}] imports multiple elements.
  • The import format can be specified with Import["file", "PGM"] or Import["file", {"PGM", elem, ...}].
  • Export["file.pgm", expr, elem] creates a PGM file by treating expr as specifying element elem.
  • Export["file.pgm", {expr1, expr2, ...}, {{elem1, elem2, ...}}] treats each expri as specifying the corresponding elemi.
  • Export["file.pgm", expr, opt1->val1, ...] exports expr with the specified option elements taken to have the specified values.
  • Export["file.pgm", {elem1->expr1, elem2->expr2, ...}, "Rules"] uses rules to specify the elements to be exported.
  • See the reference pages for full general information on Import and Export.
  • In the notebook front end, Insert ► Picture and the Open menu allow import of a PGM file into a cell.
  • Save Selection As exports the selected part of a notebook as a PGM file.
"Elements" list of elements and options available in this file
"Rules"full list of rules for each element and option
"Options"list of rules for options, properties and settings
  • Data representation elements:
"Data"array of grayscale values
"Graphics"Graphics object representing an image
"GrayLevels"equivalent to "Data"
"RGBColorArray"array of RGBColor objects
  • Import and Export by default use the "Graphics" element for PGM files.
"ImageSize"raster dimensions
  • General option:
ImageSizeAutomaticoverall image size
"ColorMap"Nonedetermines the grayscale palette used for representing the imported PGM
"DitheringMethod""FloydSteinberg"the dithering method used when color reduction is performed on Import
  • The following settings can be given for "ColorMap":
Automaticchooses an adaptive set of at most 256 colors
nadaptive set of at most n colors
{color1, color2, ...}list of GrayLevel objects
Noneno color reduction is performed
  • The "DitheringMethod" option specifies the dithering algorithm used for generating the color-reduction palette on Import:
"FloydSteinberg"Floyd-Steinberg error diffusion dithering
Noneno dithering is used
"ImageTopOrientation"Toporientation of the exported image
  • Detailed property:
"ColorSpace"GrayLevelcolor space of the image