GIF (.gif)

Background & Context

    • Registered MIME type: image/gif
    • GIF raster image format.
    • Commonly used for static and animated graphics on the web.
    • GIF is an acronym for Graphics Interchange Format.
    • Binary format.
    • Uses LZW lossless compression after restriction to an 8bit color palette.
    • Supports transparency.
    • Developed in 1987 by CompuServe.

Import & Export

  • Import["file.gif"] imports a GIF file, returning a single Image object or a list of images.
  • Import["file.gif",elem] imports the specified element from a GIF file.
  • The import format can be specified with Import["file","GIF"] or Import["file",{"GIF",elem,}].
  • Export["file.gif",expr] exports graphics, images, or any other expression to GIF.
  • Export["file.gif",{expr1,expr2,}] exports a list of graphics or images as an animation.
  • Export["file.gif",Manipulate[]] exports an animated demonstration of a Manipulate object.
  • When exporting Wolfram Language graphics or arbitrary expressions to GIF, the resulting image has the same raster size as its displayed form.
  • Image objects are by default exported at their raster size.
  • Because GIF is a raster image format, all fonts are rasterized on export.
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Notebook Interface

  • In the notebook front end, Insert Picture and the Open menu allow import of a GIF file into a cell.
  • Save Selection As exports the selected part of a notebook as a GIF 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:
  • "Animation"animated GIF represented as an animation object
    "Data"array of RGB color values
    "Graphics"first frame of a GIF file, given as a Graphics object
    "GraphicsList"frames of an animated GIF as a list of Graphics objects
    "Image"first frame of a GIF file, given as an Image object
    "ImageList"frames of an animated GIF as a list of Image objects
    "RawData"array of color map indices
    {"Thumbnail",size}thumbnail of the first image (default size = Small)
    {"ThumbnailList",frames,size}a list of thumbnails (default frames=All, size=Small)
  • Import by default uses the "Image" element for a static GIF and "ImageList" for an animated GIF.
  • Import of an animated GIF with element "Image" gives the first frame of the animation.
  • Export by default generates an animated GIF if the expression supplied is a list of images or a Manipulate, and a static GIF otherwise.
  • Export with element "Graphics" or "Image" always generates a static GIF.
  • Advanced Import elements:
  • "AnimationRepetitions"how many times the animation is played before stopping
    "Background"background color shown in transparent image regions
    "BitDepth"bits used to represent each color channel in the file
    "Channels"the number of color channels used in the file
    "ColorMap"color reduction palette, given as a list of color values
    "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
    "GlobalColorMap"color palette used by default for individual animation frames
    "ImageCount"number of frames in an animated GIF
    "RasterSize"raster dimensions
    "Summary"summary of the file
    "SummarySlideView"slide-view summary of all frames
    "TransparentColor"color to be interpreted as transparent
  • Importing the "ColorMap" element from an animated GIF will return the color maps of all frames as a list.
  • GIF supports color palettes 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 background color
    "Previous"replace with the previous frame
    Missing["NotAvailable"]missing disposal operation

Options

  • Import and Export options:
  • ImageSizeAutomaticoverall image size
    "ImageTopOrientation"Toporientation of the image as stored in the file
    RasterSizeAutomaticdimensions of the image
  • Images are by default exported at full size, typically with some automatic quantization.
  • Export options:
  • "ColorMapLength"Automaticnumber of colors to use for quantization
    "ColorMapMode""Local"whether to create a local or global color map
    Dithering"FloydSteinberg"dithering algorithm used for generating the color-reduction table
    ImageFormattingWidth$ImageFormattingWidthtarget width when formatting an object
    "QuantizationMethod"Automatica method to use for quantization
    "TransparentColor"Automaticcolor to be interpreted as transparent
  • Possible settings for Dithering are "FloydSteinberg" or None.
  • Export options for animated GIFs:
  • "ControlAppearance""Generic"how Manipulate control elements are rendered in the exported file
    "DisplayDurations"Automaticdisplay duration for each frame in seconds
    "Interlaced"Falsewhether to store graphics in interlaced form to allow progressive rendering
    AnimationRepetitionsInfinityhow many times the animation plays before stopping
    "DisposalOperation"Automaticdisposal operation before creating the next frame
    "RemoveDuplicatedFrames"Falsewhether to remove duplicated frames
  • The following settings can be given for "ControlAppearance":
  • Automaticcaptures 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
    Noneexcludes control elements when exporting an animation
  • "DisplayDurations"->{d1,d2,} specifies the display durations for each frame in an animated GIF.
  • Possible values for "DisposalOperation" are:
  • Automatic or Noneno disposal
    "Background"replace with a background color
    "Previous"replace with the previous frame

Examples

open allclose all

Basic Examples  (4)

Import a GIF file:

Summary of the image:

Read an animated GIF as a list of images:

Import this file as an animation:

Export an image object to GIF:

Scope  (3)

This rasterizes an arbitrary expression and converts the result to a GIF file:

Import the resulting file:

Read an animated GIF as a list of images:

Import this file as an animation:

Create an animated GIF by exporting a list of Graphics expressions:

Import the resulting file as an animation:

Import Elements  (26)

Available Elements  (3)

List of all available elements:

Get image-related information as a list of rules:

Import some image options:

The "Options" element is different from the options stored in the imported image:

Data Representation  (11)

"Animation"  (1)

Import frames into an animation:

"ColorMap"  (1)

Get the color-reduction palette from the first frame of an animated GIF image:

Visualize the color-reduction palette:

Apply the color map to the raw data to create the stored image:

"Data"  (1)

Get the array of color values for each icon:

Construct images of the proper type:

"Graphics"  (1)

Import as a Graphics object:

"GraphicsList"  (1)

Get the list of graphics objects:

"GlobalColorMap"  (1)

Get the global color-reduction palette from a GIF image:

Apply the color map to the raw data to create the stored image:

"Image"  (1)

Import the file as an Image object:

This is the default import element for a GIF file containing only one frame:

"ImageList"  (1)

This is the default import element for a multiframe GIF.

Get the data as a list of Image objects:

"RawData"  (1)

Get the raw data from a GIF file:

Plot the array of raw data:

Use the color map to create the stored image:

"Thumbnail"  (1)

Import a thumbnail of the image:

"ThumbnailList"  (1)

Import a list of thumbnails from an animated GIF file:

Metadata  (12)

"AnimationRepetitions"  (1)

Get the number of repetitions of the animation that is played before stopping:

"Background"  (1)

Background color used when "DisposalOperation" for a frame is "Background":

"BitDepth"  (1)

Get the number of bits used for representing each color channel:

"Channels"  (1)

Get the number of image channels:

"ColorSpace"  (1)

Get the color encoding used on export:

Use ImageColorSpace to get the color space of an imported image:

"Comments"  (1)

Get the user comments stored in the file:

"DisplayDurations"  (1)

Get the display durations of the frames of the file given in seconds:

"DisposalOperation"  (1)

Get the disposal operations:

"ImageCount"  (1)

Get the number of images stored in the file:

"RasterSize"  (1)

Get the dimensions of the images stored in the file:

Using ImageDimensions on an imported image gives the same result:

"Summary"  (1)

Get the image summary:

"SummarySlideView"  (1)

Get the summary of all images stored in the file:

Import Options  (2)

"ImageTopOrientation"  (1)

By default, the upright image is imported:

Specify the orientation of the imported image:

RasterSize  (1)

By default, images are imported in their full resolution:

Specify the raster size of the imported BMP image:

Export Options  (12)

"AnimationRepetitions"  (1)

Specify how many times the animation plays before stopping:

"ColorMapLength"  (1)

Decrease the file size by specifying a smaller length of a color map:

"ControlAppearance"  (1)

Specify whether the control menus in Manipulate should be exported as well:

Export no controls:

Export with graphical controls:

"DisplayDurations"  (1)

Specify display duration (in seconds) for each frame of animation:

Dithering  (1)

Show the effect of dithering:

Show the areas with significant difference:

"Interlaced"  (1)

Store a graphic in interlaced form to allow progressive rendering:

"QuantizationMethod"  (1)

Show the effects of various quantization methods:

Show the areas with significant difference:

RasterSize  (1)

By default, images are exported in their full resolution:

Specify the raster size of the imported image:

"TransparentColor"  (1)

Specify the color to be interpreted as transparent:

The transparent color is replaced with Transparent:

"DisposalOperation"  (3)

Export an animated GIF with a transparent background and default "DisposalOperation":

Each frame is drawn on top of the previous one:

To get frames drawn always on the clear canvas (filled with a background color), use "DisposalOperation"->"Background":

Specify disposal operations for each frame separately:

Specify None as a disposal operation for the first frame to see the difference between the "Previous" and "Background" operations:

Possible Issues  (1)

When an animated GIF has different display durations, importing as "Animation" uses the average display duration for all frames:

Create a viewer that respects different timings: