PNG (.png)
Background & Context
-
- Registered MIME type: image/png
- PNG raster image format.
- Commonly used for graphics and photographic images on the web.
- PNG is an acronym for Portable Network Graphics.
- Binary format.
- Stores both static and animated images.
- Stores color bitmaps at a resolution of 8 or 16 bits per channel.
- Grayscale images are represented at a depth of 1, 2, 4, 8, or 16 bits per pixel.
- Supports alpha channels for 8-bit and 16-bit RGB and grayscale images.
- Has support for color-reduction palettes using 2, 4, 16 or 256 8-bit RGB colors.
- Developed in 1995 as an open and patent-free alternative to the GIF format.
- Published as international standard ISO/IEC 15948:2003 and ISO/IEC 15948:2004.
Import & Export
- Import["file.png"] imports a PNG file, returning a single Image object or a list of images.
- Import["file.png",elem] imports the specified element from a PNG file.
- The import format can be specified with Import["file","PNG"] or Import["file",{"PNG",elem,…}].
- Export["file.png",expr] exports an image, graphics, or any other expression as an image to a PNG file.
- Export["file.png",{expr1,expr2,…}] exports a list of graphics or images as an animation.
- Export["file.png",Manipulate[…]] exports an animated demonstration of a Manipulate object.
- When exporting Wolfram Language graphics or arbitrary expressions to PNG, the resulting image has the same raster size as its displayed form.
- Image objects are by default exported at their raster size.
- Because PNG is a raster image format, all fonts are rasterized on export.
- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Notebook Interface
- In the notebook front end, Insert ▶ Picture and the Open menu allow the import of a PNG file into a cell.
- Save Selection As exports the selected part of a notebook as a PNG file.
Import Elements
- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements - Data representation elements:
-
"Data" array of pixel values "Graphics" first frame of a PNG file, given as a Graphics object "Image" first frame of a PNG file, given as an Image object "RawData" array of color map indices "Thumbnail" thumbnail of the first image - Data representation elements for animated GIF files:
-
"AnimatedImage" animated PNG represented as an AnimatedImage object "Animation" animated PNG represented as an animation object {"GraphicsList",frames} frames of an animated PNG as a list of Graphics objects (default frames=All) {"ImageList",frames} frames of an animated PNG as a list of Image objects (default frames=All) {"ThumbnailList",frames} a list of thumbnails (default frames=All) - Import by default uses the "Image" element for a static PNG and "ImageList" for an animated PNG.
- Import of an animated PNG with element "Image" gives the first frame of the animation.
- Export by default generates an animated PNG if the expression supplied is a list of images or a Manipulate, and a static PNG otherwise.
- Advanced Import elements:
-
"AnimationRepetitions" how many times the animation is played before stopping "BitDepth" bits used to represent each color channel in the file "BlendOperation" whether to perform alpha blending to create the next frame "CameraTopOrientation" orientation of the camera when the picture was taken "Channels" the number of color channels used in the file "ColorMap" color reduction palette, given as a list of color values "ColorProfileData" embedded color profile, given as a ColorProfileData object "ColorSpace" color encoding used in the file "Comments" user comments stored in the file "DisplayDurations" display durations of animation frames, given in seconds "DisposalOperation" disposal operation before creating the next frame "FlashUsed" whether the flash was fired "GeoPosition" latitude and longitude represented as a GeoPosition object "GPSDateTime" image creation date and time registered by GPS "ImageCount" number of frames in an animated PNG "RasterSize" raster dimensions "RedEyeCorrection" whether a red-eye correction was performed "Summary" summary of the file "SummarySlideView" slide-view summary of all frames - PNG supports a color palette of at most 256 colors, always storing colors at a resolution of 8 bits per color channel.
- "DisposalOperation" specifies how to dispose the current frame before creating the next frame. Possible values are:
-
"None" no disposal "Background" replace with a fully transparent black background "Previous" replace with the previous frame - "BlendOperation" specifies whether to alpha blend the next frame to the current frame, after disposal.
- Metadata elements:
-
"Exif" formatted Exif (Exchange image file format) "IPTC" formatted IPTC (International Press Telecommunications Council) "XMP" formatted XMP (Extensible metadata platform) "MetaInformation" combination of all formatted metadata present in the file - Raw metadata, as stored in the file, can be imported using "RawExif", "RawXMP" and "RawIPTC" elements which are returned in an association.
- All Exif and IPTC tags can be imported individually. Common tags include:
-
"ApertureValue" lens aperture "Artist" photographer or image creator "DateTime" image creation date and time "ExposureTime" exposure time, given in seconds "FlashInfo" flash information: flash fired, red-eye correction, etc. "FNumber" F number "FocalLength" actual focal length of the lens, given in millimeters "GPSAltitude" altitude of the GPS position "GPSLatitude" latitude of the GPS position "GPSLongitude" longitude of the GPS position "Make" manufacturer of the recording equipment "Model" model name or model number of the equipment "Software" name and version of the software or firmware of the camera or image input device used to generate the image
Import Options
- Import options include:
-
ImageResolution Automatic resolution values for import "ImageTopOrientation" Automatic orientation of the image as stored in the file IncludeMetaInformation All metadata types to import and export RasterSize Automatic dimensions of the image "TakeRows" {1,-1} rows to import - Possible settings for "ImageTopOrientation" are Automatic, Top, Bottom, Left or Right.
Export Options
- Export to PNG has the same options as Rasterize, with the following additions and changes:
-
"ColorMapLength" Automatic number of colors to use for quantization ColorSpace Automatic color encoding to be used in the file "Comments" None textual information CompressionLevel Automatic compression level, given as a number between 0 and 1 Dithering "FloydSteinberg" dithering algorithm used for generating the color-reduction table "ImageTopOrientation" Automatic orientation of the image as stored in the file IncludeMetaInformation All metadata types to import and export "QuantizationMethod" Automatic quantization method used by ColorQuantize RasterSize Automatic dimensions of the image - The value of "ColorMapLength" can be a positive integer not greater than 256.
- Supported ColorSpace settings are: "Grayscale" and "RGB". Using ColorSpace->Automatic, Export attempts to create a PNG file with the same color space.
- Possible settings for Dithering are "FloydSteinberg" or None.
- Possible settings for "ImageTopOrientation" are Automatic, Top, Bottom, Left or Right.
- Possible settings for "QuantizationMethod" are Automatic, "MedianCut", "MinVariance" or "Octree".
- Export options for animated PNGs:
-
AnimationRepetitions Infinity how many times the animation plays before stopping "ControlAppearance" Automatic how Manipulate control elements are rendered in the exported file "DisplayDurations" Automatic display duration for each frame in seconds "Interlaced" False whether to store graphics in interlaced form to allow progressive rendering "RemoveDuplicatedFrames" False whether to remove duplicated frames - The following settings can be given for "ControlAppearance":
-
Automatic captures user controls exactly as seen in the notebook interface, using the style elements of the local computer system "Generic" renders graphical controls in a generic style None excludes control elements when exporting an animation - "DisplayDurations"->{d1,d2,…} specifies the display durations for each frame in an animated PNG.
Examples
open all close allBasic Examples (4)
Import a PNG file as an image:
Import["ExampleData/spikey2.png"]Import["ExampleData/spikey2.png", "Summary"]Import an animated PNG as an animation:
Import["ExampleData/pearls.png", "Animation"]Export["image.png", [image]]Scope (3)
Export a Wolfram Language expression to PNG:
str = ExportString[Nest[Subsuperscript[#, #, #]&, o, 4], "PNG"];ImportString[str]Export a list of expressions to an animated PNG:
str = ExportString[NestList[Subsuperscript[#, #, #]&, o, 4], "PNG"];ImportString[str]ImportString[str, "Animation"]Export a Manipulate as an animated PNG:
expr = Manipulate[Plot[Sin[a x], {x, 0, 2π}], {a, 1, 10, 1}]str = ExportString[expr, "PNG"];
ImportString[str, "ImageCount"]Use Rasterize to export a screenshot of the Manipulate into a PNG file:
str = ExportString[Rasterize[expr], "PNG"];
ImportString[str, "ImageCount"]Import Elements (40)
Available Elements (4)
Import["ExampleData/glasses.png", "Elements"]List of available Exif elements:
Import["ExampleData/glasses.png", {"Exif", "Elements"}]//ShortList of available IPTC elements:
Import["ExampleData/glasses.png", {"IPTC", "Elements"}]//ShortGet image-related information as a list of rules:
Import["ExampleData/glasses.png", "Rules"]//ShortData Representation (11)
"AnimatedImage" (1)
Import a multiframe PNG file as an AnimatedImage:
Import["ExampleData/pearls.png", "AnimatedImage"]"ColorMap" (1)
Generate a PNG image with a color-reduction palette:
file = Export["palette.png", Import["ExampleData/coneflower.jpg", {"Rules", {"ColorMap", "RawData"}}], "Rules"]Get the color-reduction palette from a generated image:
(map = Import[file, "ColorMap"])//ShortVisualize the color-reduction palette:
MapThread[Rule, {Range[1, 256], RGBColor@@@(map / 255.)}]Apply the color map to the raw data to create the stored image:
data = Import[file, "RawData"];Image[data /. MapThread[Rule, {Range[1, 256], map}], "Byte"]"Data" (1)
"Graphics" (1)
Import as a Graphics object:
Import["ExampleData/spikey2.png", "Graphics"]Head[%]"GraphicsList" (1)
"Image" (1)
Import as an Image object:
Import["ExampleData/spikey2.png", "Image"]Head[%]The default element is also "Image":
Import["ExampleData/spikey2.png"]"ImageList" (1)
Get the data as a list of Image objects:
Import["ExampleData/pearls.png", "ImageList"]"RawData" (1)
Get the raw data from the first frame of the animated PNG:
(data = Import["ExampleData/pearls.png", "RawData"][[1]])//ShortArrayPlot[data]Use the color map to create the stored image:
map = Import["ExampleData/pearls.png", "ColorMap"];Image[Map[map[[#]]&, data], "Byte"]"Thumbnail" (1)
Metadata (25)
"BitDepth" (1)
"BlendOperation" (1)
"CameraTopOrientation" (1)
"Channels" (1)
"ColorProfileData" (1)
Get the embedded color profile, given as a ColorProfileData object:
Import["ExampleData/spikey2.png", "ColorProfileData"]The color profile is stored in the ColorSpace option of the imported image:
Options[Import["ExampleData/spikey2.png"], ColorSpace]"ColorSpace" (1)
Get the color encoding used on export:
Import["ExampleData/spikey2.png", "ColorSpace"]Use ImageColorSpace to get the color space of an imported image:
Import["ExampleData/spikey2.png"]//ImageColorSpace"DateTime" (1)
"DisposalOperation" (1)
"Exif" (3)
Import["ExampleData/glasses.png", "Exif"]Extract an individual tag from the association:
Import["ExampleData/glasses.png", "Exif"]["Photo", "FNumber"]Individual Exif tags can also be imported directly:
Import["ExampleData/glasses.png", "FNumber"]Some Exif tags are processed to give more detailed information:
Import["ExampleData/glasses.png", "FlashInfo"]Compare with the raw "Flash" tag:
Import["ExampleData/glasses.png", "RawExif"]["Photo", "Flash"]"FlashUsed" (1)
Import a tag that shows whether the flash fired:
Import["ExampleData/glasses.png", "FlashUsed"]"FlashUsed" is a subelement of the "FlashInfo" element:
Import["ExampleData/glasses.png", "Exif"]["Photo", "FlashInfo"]Extract "FlashUsed" from the full "Exif":
Import["ExampleData/glasses.png", "Exif"]["Photo", "FlashInfo", "FlashUsed"]"GeoPosition" (2)
"GPSDateTime" (1)
"ImageCount" (1)
"IPTC" (1)
"MetaInformation" (1)
"RasterSize" (1)
Get the dimensions of the image stored in the file:
Import["ExampleData/spikey2.png", "RasterSize"]Using ImageDimensions on an imported image gives the same result:
Import["ExampleData/spikey2.png"]//ImageDimensions"RawExif" (1)
Import unprocessed Exif metadata using the "RawExif" element:
Short@Import["ExampleData/glasses.png", "RawExif"]"RawExif" returns the raw values stored in the file:
Import["ExampleData/glasses.png", "RawExif"]["Photo", "FocalLength"]Compare with the interpreted focal length:
Import["ExampleData/glasses.png", "Exif"]["Photo", "FocalLength"]"RawIPTC" (1)
"RawXMP" (1)
"Summary" (1)
"SummarySlideView" (1)
Import Options (7)
ImageResolution (1)
By default, the image resolution is extracted from the file:
Options[Import["ExampleData/spikey2.png"], ImageResolution]Specify the resolution of the imported image:
Options[Import["ExampleData/spikey2.png", ImageResolution -> 144], ImageResolution]Specify different resolutions in the
and
directions:
Options[Import["ExampleData/spikey2.png", ImageResolution -> {72, 144}], ImageResolution]"ImageTopOrientation" (1)
By default, images are imported with the native orientation stored in the file:
Import["ExampleData/glasses.png"]Specify the side that is supposed to be on the top:
Import["ExampleData/glasses.png", "ImageTopOrientation" -> Left]The correct orientation can also be achieved by rotating the image to be upright:
ImageRotate[Import["ExampleData/glasses.png"], Left -> Top]IncludeMetaInformation (3)
By default, all available metadata is imported:
Options[Import["ExampleData/pearls.png", "Image"], MetaInformation]//ShortPossible classes of metadata for PNG files are "Exif", "IPTC" and "XMP":
Options[Import["ExampleData/pearls.png", "Image"], MetaInformation][[1, 2]]//KeysImport the image with no metadata:
Options[Import["ExampleData/pearls.png", "Image", IncludeMetaInformation -> None], MetaInformation]Import the image with "XMP" metadata only:
Options[Import["ExampleData/pearls.png", "Image", IncludeMetaInformation -> {"XMP"}], MetaInformation]//ShortRasterSize (1)
Export Options (11)
AnimationRepetitions (1)
"ColorMapLength" (1)
"ControlAppearance" (1)
Specify whether the control menus in Manipulate should be exported as well:
m = Manipulate[Plot[Sin[x(1 + a x)], {x, 0, 6}], {a, 0, 2}]ImportString[ExportString[m, "PNG", "ControlAppearance" -> None], "Image"]Export with graphical controls:
ImportString[ExportString[m, "PNG", "ControlAppearance" -> "Generic"], "Image"]CompressionLevel (1)
Use higher compression levels to decrease a file size:
i = [image];Table[FileByteCount[Export["image.png", i, CompressionLevel -> l]], {l, 0, 1, .1}]ListLinePlot[%, PlotRange -> All]Higher compression levels require more time to generate a PNG file:
Table[RepeatedTiming[Export["image.png", i, CompressionLevel -> l]][[1]], {l, 0, 1, .1}]//ListLinePlot"DisplayDurations" (1)
Dithering (1)
"ImageTopOrientation" (1)
By default, the upright image is exported using "ImageTopOrientation"->Top:
i = [image];Export["image.png", i]Import[%]Export["image.png", i, "ImageTopOrientation" -> Left]Import["image.png"]Import using the original orientation:
Import["image.png", "ImageTopOrientation" -> Top]IncludeMetaInformation (1)
By default, all existing meta-information is exported:
Options[ImportString[ExportString[[image], "PNG"]], MetaInformation]//ShortKeys[%[[1, 2]]]Specify classes of metadata to export:
Options[ImportString[ExportString[[image], "PNG", IncludeMetaInformation -> {"Comments"}]], MetaInformation]Keys[%[[1, 2]]]"QuantizationMethod" (1)
RasterSize (1)
By default, the image is exported in the full resolution:
i = [image];ImageDimensions[i]Export["image.png", i]Import[%, "RasterSize"]Specify the raster size of the exported image:
Export["image.png", i , RasterSize -> 47]Import[%, "RasterSize"]"RemoveDuplicatedFrames" (1)
Create a video with duplicated frames:
v = SlideShowVideo[{[image], [image], [image]}]frames = VideoFrameList[v, All];
Length[frames]Export all duplicated frames to an animated PNG:
Export["file.png", frames]//FileSizeExport while removing duplicated frames and notice the file size reduction:
Export["file.png", frames, "RemoveDuplicatedFrames" -> True]//FileSizeTech Notes
Related Guides
History
Introduced in 2003 (5.0) | Updated in 2018 (11.3) ▪ 2020 (12.2) ▪ 2021 (12.3) ▪ 2022 (13.1) ▪ 2022 (13.2) ▪ 2024 (14.1) ▪ 2026 (15.0)