Pillow ImageDraw Module

ImageDraw is a module in Pillow (a fork of PIL), a Python image processing library, used for drawing various shapes and text on images.

ImageDraw provides rich drawing capabilities, including lines, rectangles, circles, polygons, text, etc., suitable for image annotation, generating charts, or simple graphic design.

ImageDrawThe main features of the module include:

  • Supports drawing on existing images
  • Can create brand-new blank canvases
  • Provides multiple drawing primitives (points, lines, rectangles, ellipses, etc.)
  • Supports text drawing and font settings
  • Allows custom drawing colors and styles

Basic Usage of ImageDraw

Creating a Draw Object

Before using ImageDraw, you need to first create aImageDraw.Drawobject, passing in anImageobject as the basis for drawing.

Example

from PIL import Image, ImageDraw

# Create a blank image (white background)
image = Image.new("RGB", (400, 300), "white")

# Create a Draw object
draw = ImageDraw.Draw(image)

Drawing Basic Shapes

ImageDraw supports multiple drawing methods, such as:

  • Draw lines:line()
  • Draw rectangles:rectangle()
  • Draw circles/ellipses:ellipse()
  • Draw polygons:polygon()
  • Draw text:text()

Common ImageDraw Methods

Method Description Example
line(xy, fill=None, width=1) Draw a line,xyis a coordinate tuple ((x1, y1, x2, y2)) draw.line([(10, 10), (100, 50)], fill="red", width=2)
rectangle(xy, fill=None, outline=None, width=1) Draw a rectangle,xyare the coordinates of the upper-left and lower-right corners draw.rectangle([(50, 50), (150, 150)], fill="blue", outline="black")
ellipse(xy, fill=None, outline=None, width=1) Draw an ellipse or circle draw.ellipse([(100, 100), (200, 200)], fill="green")
polygon(xy, fill=None, outline=None) Draw a polygon draw.polygon([(50, 50), (100, 20), (150, 50)], fill="yellow")
text(xy, text, fill=None, font=None) Draw text on an image draw.text((50, 50), "Hello Pillow!", fill="black")
arc(xy, start, end, fill=None, width=1) Draw an arc draw.arc([(50, 50), (150, 150)], 0, 90, fill="red")
chord(xy, start, end, fill=None, outline=None) Draw a chord (arc + connecting line) draw.chord([(50, 50), (150, 150)], 0, 180, fill="blue")
pieslice(xy, start, end, fill=None, outline=None) Draw a pie slice draw.pieslice([(50, 50), (150, 150)], 0, 90, fill="green")
textbbox() Used to calculate the bounding box occupied by given text when drawn on an image draw.textbbox((50, 50), "Hello Pillow!", font=font)

Comprehensive Example

The following is a complete example showing how to use ImageDraw to draw multiple shapes and text:

Example

from PIL import Image, ImageDraw, ImageFont

# Create a blank image
image = Image.new("RGB", (500, 400), "white")
draw = ImageDraw.Draw(image)

# Draw a line
draw.line([(10, 10), (490, 10)], fill="red", width=3)

# Draw a rectangle
draw.rectangle([(50, 50), (150, 150)], fill="blue", outline="black")

# Draw a circle
draw.ellipse([(200, 50), (300, 150)], fill="green")

# Draw a polygon
draw.polygon([(350, 50), (400, 100), (450, 50), (400, 150)], fill="yellow")

# Draw text
font = ImageFont.load_default()
draw.text((50, 200), "Hello Pillow!", fill="black", font=font)

# Save the image
image.save("draw_example.png")

After running, an image file containing multiple shapes will be generated.draw_example.png。


Summary

ImageDraw is a very practical drawing module in Pillow, suitable for:

  • Image annotation (such as adding text, arrows)
  • Generating simple charts or diagrams
  • Custom shape drawing

By masteringline()、rectangle()、ellipse()、text()and other methods, you can easily fulfill various drawing needs.

If you need more advanced drawing features, you can combineImageFont(font settings) orImageFilter(filter effects) to further expand functionality.

I hope this tutorial helps you quickly get started with Pillow's ImageDraw module!

Other Extensions