Python ImageDraw Module



Python ImageDraw module provides basic graphics support for Image objects. When you would like to draw on image then ImageDraw is the module that fits for this requirement.

Python ImageDraw module has various methods like line, rectangle, ellipse, and text etc for drawing on the Image object or on the newly created image.

Following is the syntax to import ImageDraw module:

from PIL import ImageDraw

Module Functions

The ImageDraw.Draw() Function

This Draw function creates an object that can be used to draw in the given image.

Syntax

Following is the syntax of ImageDraw.Draw() function:

ImageDraw.Draw(im, mode=None)

Below is the detail about parameters used:

  • im This is the Image object you want to draw on.
  • mode This can be RGB or RGBA (to blend the drawing into the image ). If it is left blank, the default mode is taken from the image object.

Example

from PIL import Image, ImageDraw

im = Image.new("RGB", (600, 250))

draw = ImageDraw.Draw(im)

Above example will create an image object using RGB black color and this image object is being used to create a draw object which can be used to draw different shapes.

Module Methods

The Draw.arc() Method

This method draws an arc between the start and end angles, inside the given bounding box.

Syntax

Following is the syntax of Draw.arc() method:

Draw.arc(xy, start, end, fill=None)

Below is the detail about parameters used:

  • xy - This represents to four points to define the bounding box. For example [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
  • start - This is the starting angle, in degrees. Angles increase clockwise. For example 100 or 200 etc.
  • end - This is the ending angle, in degrees. For example
  • fill - This is the color to use for the arc. For example (255,255,255,128). The default color is white.

Example

from PIL import Image, ImageDraw

im = Image.new("RGB", (600, 250))

draw = ImageDraw.Draw(im)

draw.arc((50, 50,200,200),0, 270, fill=(255,255,255,128))
del draw

# Finally save image into a file
im.save("pillow.png", "PNG")

Above code will produce following result:

PIL Arc

The Draw.bitmap() Method

This method draws a bitmap (mask) at the given position, using the current fill color for the non-zero portions. The bitmap should be a valid transparency mask.

Syntax

Following is the syntax of Draw.bitmap() method:

Draw.bitmap(xy, bitmap, fill=None)

Below is the detail about parameters used:

  • xy - This represents to four points to define the bounding box. For example [(x0, y0), (x1, y1)] or [x0, y0, x1, y1].
  • bitmap - The bitmap should be a valid transparency mask (mode 1) or matte (mode L or RGBA)
  • fill - This is the color to use for the arc. For example (255,255,255,128). The default color is white.

Example

from PIL import Image, ImageDraw

im = Image.new("RGB", (600, 250))

draw = ImageDraw.Draw(im)

# draw.bitmap((50, 50,200,200),???, fill=(255,255,255,128))
del draw

# Finally save image into a file
im.save("pillow.png", "PNG")

Above code will produce following result:

PIL Arc
Advertisements