From Wikimedia Commons, the free media repository
Jump to: navigation, search

This page gives an overview of several techniques how to generate images with automated scripts. Many technical images or drawings are well defined in geometric terms and it is much better to create them with an automated computer program than drawing them by hand. Advantages are

  • Possible faster image generation
  • More exact and well defined images
  • It is very simple to create altered versions or animations

Since the rendering of images is in most cases not time critical at all, it is best to use a slow but powerful programming language that will allow for rapid programming such as Python.

Generating SVG vector graphics[edit]

SVG vector graphics with lxml etree[edit]

This is a very low-level method. SVG vector graphics are xml documents, therefore using an xml library such as Lxml.etree is already a great enhancement over plain text generation.

Python XML etree example script:

example image created with lxml etree

SVG vector graphics with python svgwrite[edit]

Svgwrite [1] offers a very convenient way to create svg graphics from python.

Python svgwrite example script:

example images created with python svgwrite
Poincare-sphere arrows.svg
Poincare sphere
Ehrenfest paradox
Cylindrical magnet.svg
Cylindrical magnet

SVG vector graphics with cairo[edit]

The Cairo library provides powerful drawing techniques with SVG export. However the control of how the output file will internally look like is more restricted with this method. For instance, text is automatically converted to paths.

Python cairo SVG example script:

SVG vector graphics with Matplotlib[edit]

The Matplotlib plotting library can be used to create arbitrary vector drawings not necessarily related to plots. The usage is rather high-level and provides many specific drawing functions.

Python Matplotlib SVG example script:

Drawing raster images[edit]

Raster images with PIL[edit]

The Python Image Library (PIL) provides very basic drawing capabilities. PIL is actually better suited for pixel-wise image manipulation.

Python PIL example script:

Raster images with cairo[edit]

Cairo is a very powerful graphics library and can be accessed from python.

Python cairo raster example script:

cairo raster image example

gif Animations with Matplotlib[edit]

Matplotlib has not only excellent graph plottling capabilites, but it can also be used to create arbitrary gif animations with relatively low effort.

Python Matplotlib raster example script:

Matplotlib animation example

Pixel-wise image generation[edit]

Pixel-wise image generation with Netpbm[edit]

Netpbm or PNM is a simplistic format to create ASCII and binary images. It does not require any additional libraries, which maximizes the portability of scripts using it. PNM images are understood by all decent graphics programs and they can be converted to a more common raster image format such as PNG with free graphics software like GIMP or the command line program pnmtopng.

Python PNM example script:

Pixel-wise image generation with Scipy[edit]

Python SciPy offers PNG image export from raw NumPy data arrays. This opens the convenient possibility to create pixe-wise defined images from scratch.

Python Scipy example script:

Pixel-wise images with PIL[edit]

The Python Image Library (PIL) provides very basic drawing capabilities. PIL is actually better suited for pixel-wise image manipulation.

Python PIL example script:

example pixel-wise images created with PIL
Airydisks rayleigh sqrt.png
Rayleigh criterion
Hydrogen eigenstate n4 l3 m1.png
Hydrogen orbital

Pixel-wise gif Animations with Matplotlib[edit]

Matplotlib can also be used for animations that are defined per pixel. We simply need to fill a 2D array of color values and then pass it to Matplotlib via imshow().

Python Matplotlib pixel-wise raster example script:

Matplotlib pixelwise animation example

Assembling animations[edit]

gifsicle animation example

Creating animations in the GIF format is easy once you created a series of still-images. All you need is the free program gifsicle. If the still images are available in another format such as PNG or SVG, ImageMagick can do the conversion. The following commands can be executed in the command line shell such as the bash or by using cygwin. The most convenient way is to embed the commands in a python script using os.system(command).

Convert all still frames to GIF:

for i in *.svg;
  do convert "$i" "$(echo $i | sed s/\\\.svg/\\\.gif/)";

Combine the GIF frames to an animation:

gifsicle -d5 -l0 *.gif > animation.gif