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.
  • Import and Export fully support the PGM format.
  • Export creates binary PGM files.
  • If multiple images are present in a PGM file, Import returns the first of the images.

Import and ExportImport and Export

  • Import["file.pgm"] imports a PGM file as an image.
  • Export["file.pgm", expr] exports an image, graphics, or any other expression as a raster image to PGM.
  • Export by default generates binary PGM files.
  • When exporting Mathematica graphics or arbitrary expressions to PGM, the resulting image has the same raster size as its displayed form.
  • Image objects are by default exported at their raster size.
  • 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 as specifying the corresponding .
  • 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.
  • ImportString and ExportString support the PGM format.

Notebook InterfaceNotebook Interface

  • 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.

ElementsElements

  • General Import elements:
  • "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"array of normalized gray-level values
    "Image"raster image, given as an Image object
    "RGBColorArray"array of RGBColor objects
  • Import and Export by default use the element for PGM files.
  • Advanced Import elements:
  • "BitDepth"bits used to represent each color channel in the file
    "ColorSpace"color space of the image
    "DataType"data type used for storing pixel elements
    "ImageSize"raster dimensions

OptionsOptions

ExamplesExamplesopen allclose all

Basic Examples (3)Basic Examples (3)

This imports a PGM example file:

In[1]:=
Click for copyable input
Out[1]=

Import the element:

In[2]:=
Click for copyable input
Out[2]=

Export a gray-level image to PGM:

In[1]:=
Click for copyable input
Out[1]=

Export arbitrary graphics to the PGM format:

In[1]:=
Click for copyable input
Out[1]=
New in 4 | Last modified in 9
New to Mathematica? Find your learning path »
Have a question? Ask support »