# 30. `gui.image` — Saving OpenGL renderings to image files.

This module defines some functions that can be used to save the OpenGL rendering and the pyFormex GUI to image files. There are even provisions for automatic saving to a series of files and creating a movie from these images.

## 30.1. Functions defined in module gui.image

`gui.image.``imageFormats`(mode='w')

  Return a list of the valid image formats.

  image formats are lower case strings as ‘png’, ‘gif’, ‘ppm’, ‘eps’, etc. The available image formats are derived from the installed software.

  Parameters:

  - mode: ‘w’ or ‘r’, for formats that can be written or read.

`gui.image.``initialize`()

  Initialize the image module.

`gui.image.``checkImageFormat`(fmt, verbose=True)

  Checks image format; if verbose, warn if it is not.

  Returns the image format, or None if it is not OK.

`gui.image.``imageFormatFromExt`(ext)

  Determine the image format from an extension.

  The extension may or may not have an initial dot and may be in upper or lower case. The format is equal to the extension characters in lower case. If the supplied extension is empty, the default format ‘png’ is returned.

`gui.image.``save_canvas`(canvas, fn, fmt='png', quality=-1, size=None, alpha=False)

  Save the rendering on canvas as an image file.

  canvas specifies the qtcanvas rendering window. fn is the name of the file fmt is the image file format

`gui.image.``save_window`(filename, format, quality=-1, windowname=None, crop=None)

  Save a window as an image file.

  This function needs a filename AND format. If a window is specified, the named window is saved. Else, the main pyFormex window is saved.

  If crop is given, the specified rectangle is cropped. If crop=’canvas’, windowname is set to main pyFormex window and crop to canvas rectangle.

  In all cases, the GUI and current canvas are raised.

`gui.image.``save_window_rect`(filename, format, quality=-1, window='root', crop=None)

  Save a rectangular part of the screen to a an image file.

  crop: (x,y,w,h)

`gui.image.``save`(filename=None, window=False, multi=False, hotkey=True, autosave=False, border=False, grab=False, format=None, quality=-1, size=None, verbose=False, alpha=True, rootcrop=None)

  Saves an image to file or Starts/stops multisave mode.

  With a filename and multi==False (default), the current viewport rendering is saved to the named file.

  With a filename and multi==True, multisave mode is started. Without a filename, multisave mode is turned off. Two subsequent calls starting multisave mode without an intermediate call to turn it off, do not cause an error. The first multisave mode will implicitely be ended before starting the second.

  In multisave mode, each call to saveNext() will save an image to the next generated file name. Filenames are generated by incrementing a numeric part of the name. If the supplied filename (after removing the extension) has a trailing numeric part, subsequent images will be numbered continuing from this number. Otherwise a numeric part ‘-000’ will be added to the filename.

  If window is True, the full pyFormex window is saved. If window and border are True, the window decorations will be included. If window is False, only the current canvas viewport is saved.

  If grab is True, the external ‘import’ program is used to grab the window from the screen buffers. This is required by the border and window options.

  If hotkey is True, a new image will be saved by hitting the ‘S’ key. If autosave is True, a new image will be saved on each execution of the ‘draw’ function. If neither hotkey nor autosave are True, images can only be saved by executing the saveNext() function from a script.

  If no format is specified, it is derived from the filename extension. fmt should be one of the valid formats as returned by imageFormats()

  If verbose=True, error/warnings are activated. This is usually done when this function is called from the GUI.

`gui.image.``saveImage`(filename=None, window=False, multi=False, hotkey=True, autosave=False, border=False, grab=False, format=None, quality=-1, size=None, verbose=False, alpha=True, rootcrop=None)

  Saves an image to file or Starts/stops multisave mode.

  With a filename and multi==False (default), the current viewport rendering is saved to the named file.

  With a filename and multi==True, multisave mode is started. Without a filename, multisave mode is turned off. Two subsequent calls starting multisave mode without an intermediate call to turn it off, do not cause an error. The first multisave mode will implicitely be ended before starting the second.

  In multisave mode, each call to saveNext() will save an image to the next generated file name. Filenames are generated by incrementing a numeric part of the name. If the supplied filename (after removing the extension) has a trailing numeric part, subsequent images will be numbered continuing from this number. Otherwise a numeric part ‘-000’ will be added to the filename.

  If window is True, the full pyFormex window is saved. If window and border are True, the window decorations will be included. If window is False, only the current canvas viewport is saved.

  If grab is True, the external ‘import’ program is used to grab the window from the screen buffers. This is required by the border and window options.

  If hotkey is True, a new image will be saved by hitting the ‘S’ key. If autosave is True, a new image will be saved on each execution of the ‘draw’ function. If neither hotkey nor autosave are True, images can only be saved by executing the saveNext() function from a script.

  If no format is specified, it is derived from the filename extension. fmt should be one of the valid formats as returned by imageFormats()

  If verbose=True, error/warnings are activated. This is usually done when this function is called from the GUI.

`gui.image.``saveNext`()

  In multisave mode, saves the next image.

  This is a quiet function that does nothing if multisave was not activated. It can thus safely be called on regular places in scripts where one would like to have a saved image and then either activate the multisave mode or not.

`gui.image.``changeBackgroundColorXPM`(fn, color)

  Changes the background color of an .xpm image.

  This changes the background color of an .xpm image to the given value. fn is the filename of an .xpm image. color is a string with the new background color, e.g. in web format (‘#FFF’ or ‘#FFFFFF’ is white). A special value ‘None’ may be used to set a transparent background. The current background color is selected from the lower left pixel.

`gui.image.``saveIcon`(fn, size=32, transparent=True)

  Save the current rendering as an icon.

`gui.image.``autoSaveOn`()

  Returns True if autosave multisave mode is currently on.

  Use this function instead of directly accessing the autosave variable.

`gui.image.``createMovie`(files, encoder='convert', outfn='output', **kargs)

  Create a movie from a saved sequence of images.

  Parameters:

  - files: a list of filenames, or a string with one or more filenames separated by whitespace. The filenames can also contain wildcards interpreted by the shell.
  - encoder: string: the external program to be used to create the movie. This will also define the type of output file, and the extra parameters that can be passed. The external program has to be installed on the computer. The default is convert, which will create animated gif. Other possible values are ‘mencoder’ and ‘ffmeg’, creating meg4 encode movies from jpeg input files.
  - outfn: string: output file name (not including the extension). Default is output.

  Other parameters may be passed and may be needed, depending on the converter program used. Thus, for the default ‘convert’ program, each extra keyword parameter will be translated to an option ‘-keyword value’ for the command.

  Example:

      createMovie('images*.png',delay=1,colors=256)

  will create an animated gif ‘output.gif’.

`gui.image.``saveMovie`(filename, format, windowname=None)

  Create a movie from the pyFormex window.