Drawing on Images with Python and Pillow (Part II)
Last week, you mastered the standard parameters necessary to draw lines on images with Python and Pillow. This week, you're taking it a step further.
In this newsletter, you will learn how to draw using other shapes. Specifically, you will learn the following:
Drawing Arcs
Drawing Chords
Drawing Ellipses
This is the second part of a series about drawing shapes on images in Python. The information in this tutorial comes from my book, Pillow: Image Processing with Python.
Let’s get started!
Drawing Arcs
An arc is a curved line. You can draw arcs with Pillow too. Here is the arc()
method specification:
An arc()
can also be made using xy
points. The start
parameter defines the starting angle in degrees. The end parameter tells Pillow what the ending angle is, also in degrees. The other two parameters have already been introduced.
To see how you might draw an arc, create a new file named draw_arc.py
and add this code to it:
In this code, you create a new image with a white background. Then, you create your Draw object. Next, you create two different arcs. The first arc will be filled with green. The second arc will be filled in yellow, but its line width will be 5. When you draw an arc, the fill refers to the arc’s line color. You aren’t filling the arc itself.
When you run this code, your output image will look like this:
Try changing some of the parameters and re-running the code to see how to change the arcs yourself.
Now let’s move on and learn about drawing chords!
Drawing Chords
Pillow also supports the concept of chords. A chord is the same as an arc except that the endpoints are connected with a straight line.
Here is the method definition of chord()
:
The only difference here is that you can also add an outline
color. This color can be specified in any of the ways that you can specify a fill
color.
Create a new file and name it draw_chord.py
. Then add this code so you can see how you make chords yourself:
This example will draw two chords on a green image. The first chord is filled in with red. The second chord is filled in with yellow but outlined in blue. The blue outline has a width of 5.
When you run this code, you will create the following image:
That looks pretty good. Go ahead and play around with this example, too. You’ll soon master chord-making with Pillow with a little practice.
Now let’s continue and learn about drawing ellipses!
Drawing Ellipses
An ellipse, or oval, is drawn in Pillow by giving it a bounding box (xy). You have seen this several other times in previous sections.
Here is the ellipse()
method definition:
The ellipse()
lets you fill it with a color, add a colored border (outline
) and change the width
of that outline
.
To see how you can create an ellipse()
, make a new file named draw_ellipse.py
and add this code to it:
In this code, you create a nice white image via the new()
method. Then, you draw a red ellipse on top of it. Finally, you draw a second ellipse filled with yellow and outlined in black, with the outline width set to 5.
When you run this code, the image it creates will look like this:
You can create ovals and circles using ellipse()
. Give it a try and see what you can do with it.
Now let’s find out how to create pie slices!
Drawing Pie Slices
A pie slice is the same as arc())
, but also draws straight lines between the endpoints and the center of the bounding box.
Here is how the pieslice()
method is defined:
You have used all of these parameters in other drawings. To review, fill
adds color to the inside of the pieslice()
while outline
adds a colored border to the figure.
To start practicing this shape, create a new file named draw_pieslice.py
and add this code to your file:
In this code, you generate a grey image to draw on. Then you create two pie slices. The first pieslice()
is filled in with green. The second one is not filled in but has a yellow outline
. Note that each pieslice()
has a different starting and ending degree.
When you run this code, you will get the following image:
With a little work, you could create a pie graph using Pillow! You should play around with your code a bit and change some values. You will quickly learn how to make some nice pie slices.
Wrapping Up
You learned how to draw three new shapes in this tutorial. Specifically, you learned about the following:
Drawing Arcs
Drawing Chords
Drawing Ellipses
Next week, you will finish up this three-part series when you learn how to draw pie slices, polygons, and rectangles! See you then!