ImageDraw
ImageDraw module provides support for simple 2D graphics. The graphics commands support shape drawing and text annotation.
- An image can be considered to be a two-dimensional array of pixels (picture elements). A pixel is the smallest dot of color being supported,
- The two-dimensional coordinate system used by ImageDraw has the origin in the upper left corner of the image.
- The Pillow color scheme we use is RGB. The module ImageColor provides color RGB representation and support.
- The acceptable fonts for text annotations are bitmap, OpenType, or TrueType.
- Many of the drawing commands require a bounding box parameter that specifies the area on the image to which the command is to be applied. A bounding box is specified by giving the upper left and lower right corners of the rectangular canvas subregion being draw on. There are are two supported bounding box representations.
Suppose coordinates
(x0, y0
and(x1, y1)
are respectively the upper left and lower right box coordimates of interest. The bounding box representation for those two corners is[ (xo, y0), (x1, y1) ]
.
- A sequence of coordinates can be represented as
[ (xo, y0), (x1, y1), ... ]
.
- Some drawing commands require angle values. The origin angle is that of the little hand for analog clock whose time at 3 o'clock.
Canvas
- A Pillow drawable surface (i.e., a canvas) of an
Image
is anImageDraw
.
ImageDraw.Draw( im )
returns a drawable canvas representation ofImage
parameterim
. The background of the canvas is theim
image.
Drawing
- Suppose below
canvas
is a drawable image.
canvas.line( xy, fill=None, width=0 )
- Draws a line from the upper left to lower right corners of bounding box
xy
oncanvas
. The line is filled using colorfill
. Parametersfill
andwidth
are optional with default values respectively ofNone
and0
.
canvas.rectangle( xy, fill=None, outline=None )
- Draws the rectangle given bounding box
xy
oncanvas
. The shape is filled using colorfill
and the perimeter in coloroutline
. Parametersfill
andoutline
are optional with default valuesNone
.
canvas.ellipse( xy, fill=None, outline=None )
- Draws the ellipse surrounded by bounding box
xy
oncanvas
. The shape is filled using colorfill
and the perimeter in coloroutline
. Parametersfill
andoutline
are optional with default valuesNone
.
canvas.point( seq, fill=None )
- Draws pixels (points) at coordinate sequence locations
seq
oncanvas
. The pixels are filled using colorfill
. Parameterfill
is optional with default valueNone
.
canvas.polygon( seq, fill=None, outline=None )
- Draws a polygon connecting with straight lines the coordinate sequence locations
seq
oncanvas
. The first and last coordinates inseq
are also connected by a straight-line. The shape is filled using colorfill
and the perimeter in coloroutline
. Parametersfill
andoutline
are optional with default valuesNone
.
canvas.arc( xy, sa, ea, fill=None )
- Draws an arc within bounding box
xy
oncanvas
. The arc outline has starting anglesa
and ending angleea
. The shape is drawn using colorfill
. Parameterfill
is optional with default valueNone
.
canvas.chord( xy, sa, ea, fill=None, outline=None )
- Draws a chord within bounding box
xy
oncanvas
. The chord arc has starting anglesa
and ending angleea
and the arc endpoints are connected by a straight-line. The shape is filled using colorfill
and the perimeter in coloroutline
. Parametersfill
andoutline
are optional with default valuesNone
.
canvas.pieslice( xy, sa, ea, fill=None, outline=None )
- Draws a pie slice within bounding box
xy
oncanvas
. The pie slice arc has starting anglesa
and ending angleea
. Lines are drawn from the arc endpoints to the center of the bounding box. The shape is filled using colorfill
and the perimeter in coloroutline
. Parametersfill
andoutline
are optional with default valuesNone
.
canvas.text( loc, s, fill=None, font=None, anchor=None, spacing=0, align='left')
- Writes string
s
at coordinateloc
oncanvas
. The text is written using colorfill
in fontfont
with line spacingspace
and alignmentalign
. Parametersfill
,font
,spacing
, andalign
are optional with default values respectively ofNone
,None
,0
, andleft
. The possible values foralign
are'left
','center
' and'right
'.