scallop dome pyformex logo

Previous topic

27. camera — OpenGL camera handling

Next topic

29. imageViewer — A general image viewer

[FSF Associate Member]

Valid XHTML 1.0 Transitional

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

Classes defined in module image

Functions defined in module image

image.initialize()

Initialize the image module.

image.imageFormats()

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.

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.

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.

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

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

image.save_window(filename, format, quality=-1, windowname=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.

image.save_main_window(filename, format, quality=-1, border=False)

Save the main pyFormex window as an image file.

This function needs a filename AND format. This is an alternative for save_window, by grabbin it from the root window, using save_rect. This allows us to grab the border as well.

image.save_rect(x, y, w, h, filename, format, quality=-1)

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

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

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

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

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

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.

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.

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

Save the current rendering as an icon.

image.autoSaveOn()

Returns True if autosave multisave mode is currently on.

Use this function instead of directly accessing the autosave variable.

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

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

Create a movie from the pyFormex window.