The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.
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")
The graphics interface uses the same coordinate system as PIL itself, with (0, 0) in the upper left corner.
To specify colours, you can use numbers or tuples just as you would use with Image.new or Image.putpixel. For “1”, “L”, and “I” images, use integers. For “RGB” images, use a 3-tuple containing integer values. For “F” images, use integer or floating point values.
For palette images (mode “P”), use integers as colour indexes. In 1.1.4 and later, you can also use RGB 3-tuples or colour names (see below). The drawing layer will automatically assign colour indexes, as long as you don’t draw with more than 256 colours.
In PIL 1.1.4 and later, you can also use string constants when drawing in “RGB” images. PIL supports the following string formats:
PIL can use bitmap fonts or OpenType/TrueType fonts.
Bitmap fonts are stored in PIL’s own format, where each font typically consists of a two files, one named .pil and the other usually named .pbm. The former contains font metrics, the latter raster data.
To load a bitmap font, use the load functions in the ImageFont module.
To load a OpenType/TrueType font, use the truetype function in the ImageFont module. Note that this function depends on third-party libraries, and may not available in all PIL builds.
Creates an object that can be used to draw in the given image.
Note that the image will be modified in place.
Draws an arc (a portion of a circle outline) between the start and end angles, inside the given bounding box.
The outline option gives the colour to use for the arc.
Draws a bitmap (mask) at the given position, using the current fill colour for the non-zero portions. The bitmap should be a valid transparency mask (mode “1”) or matte (mode “L” or “RGBA”).
This is equivalent to doing image.paste(xy, color, bitmap).
To paste pixel data into an image, use the paste method on the image itself.
Same as arc(), but connects the end points with a straight line.
The outline option gives the colour to use for the chord outline. The fill option gives the colour to use for the chord interior.
Draws an ellipse inside the given bounding box.
The outline option gives the colour to use for the ellipse outline. The fill option gives the colour to use for the ellipse interior.
Draws a line between the coordinates in the xy list.
The coordinate 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.
(New in 1.1.5) The width option gives the line width, in pixels. Note that line joins are not handled well, so wide polylines will not look good.
Same as arc, but also draws straight lines between the end points and the center of the bounding box.
The outline option gives the colour to use for the pieslice outline. The fill option gives the colour to use for the pieslice interior.
Draws points (individual pixels) at the given coordinates.
The coordinate 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.
Draws a polygon.
The polygon outline consists of straight lines between the given coordinates, plus a straight line between the last and the first coordinate.
The coordinate 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.
Draws a rectangle.
The box can be any sequence object containing either 2-tuples [ (x, y), (x, y) ] or numeric values [ x, y, x, y ]. It should contain 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.
Draws the string at the given position. The position gives the upper left corner of the text.
The font option is used to specify which font to use. It should be an instance of the ImageFont class, typically loaded from file using the load method in the ImageFont module.
The fill option gives the colour to use for the text.
Return the size of the given string, in pixels.
The font option is used to specify which font to use. It should be an instance of the ImageFont class, typically loaded from file using the load method in the ImageFont module.