The ImageDraw Module

Draft (Updated 1999-02-12)

This module provide basic graphics support for Image objects. It can for example be used to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.

Example

Example: Draw a Grey Cross Over an Image

import Image, ImageDraw

im = Image.open("lena.pgm")

draw = ImageDraw.Draw(im)
draw.line((0, 0), im.size, fill=128)
draw.line((0, im.size[1]), (im.size[0], 0), fill=128)
del draw 

# write to stdout
im.save(sys.stdout, "PNG")

Functions

Draw (constructor)

Draw(image). Creates an object that can be used to draw in the given image. The image will be modified in place.

Methods

arc

arc(xy, start, end, options). [FIXME]

bitmap

arc(xy, bitmap, options). [FIXME]

chord

chord(xy, start, end, options). [FIXME]

The outline option gives the colour to use for the chord outline. The fill option gives the colour to use for the chord interior.

ellipse

ellipse(xy, options). [FIXME]

The outline option gives the colour to use for the ellipse outline. The fill option gives the colour to use for the ellipse interior.

line

line(xy, options). Draw a line between the coordinates in the xy list. The list can be any sequence object containing either 2-tuples [ (x, y), ... ] or numeric values [ x, y, ... ]. It should contain at least two coordinates.

The fill option gives the colour to use for the line.

pieslice

pieslice(xy, start, end, options). [FIXME]

The outline option gives the colour to use for the pieslice outline. The fill option gives the colour to use for the pieslice interior.

point

point(xy, options). Draws point at the given coordinates. The list can be any sequence object containing either 2-tuples [ (x, y), ... ] or numeric values [ x, y, ... ].

The fill option gives the colour to use for the points.

polygon

polygon(xy, options). Draws a polygon whose outline consists of straight lines between the given coordinates, plus a straight line between the last and the first coordinate.

The list can be any sequence object containing either 2-tuples [ (x, y), ... ] or numeric values [ x, y, ... ]. It should contain at least three coordinates.

The outline option gives the colour to use for the polygon outline. The fill option gives the colour to use for the polygon interior.

rectangle

rectangle(box, options). Draws a rectangle.

The list can be any sequence object containing either 2-tuples [ (x, y), (x, y) ] or numeric values [ x, y, x, y ]. It should contain exactly two coordinates.

Note that the second coordinate pair defines a point just outside the rectangle, also when the rectangle is not filled.

The outline option gives the colour to use for the rectangle outline. The fill option gives the colour to use for the rectangle interior.

text

text(position, string, options). Draws the string at the given position.

textsize

textsize(string, options). Return the size of the given string, as a 2-tuple.

Compatibility

The Draw class contains a constructor and a number of methods which are provided for backwards compatibility only. For this to work properly, you should either use options on the drawing primitives, or these methods. Do not mix the old and new calling conventions.

ImageDraw (constructor)

ImageDraw(image). Same as Draw. Should not be used in new code.

setink

setink(ink). Selects the pixel value wto use with subsequent operations.

setfill

setfill(onoff). Selects if subsequently drawn shapes (like polygons and rectangles) should be filled or just outlined.

setfont

setfont(font). Selects the font to use for the text method. The font argument should be an instance of the ImageFont class, typically loaded from file using the load method in the ImageFont module.