Python Pillow - ImageDraw.arc() Function



In geometry, an arc is a segment or a part of the circumference of a circle, defined by two points on the circle known as endpoints, and the continuous curve between these points. In the context of image processing, the Python Pillow library provides the arc() method within its ImageDraw module to draw arcs on images using a class called Draw().

The ImageDraw.arc() method is used to draw an arc (which is a portion of a circle outline) inside a specified bounding box.

Syntax

Following is the syntax of the function −

ImageDraw.arc(xy, start, end, fill=None, width=0)

Parameters

Here are the details of this function parameters −

  • xy − Define the bounding box in Two points. This can be provided as a sequence of [(x0, y0), (x1, y1)] or [x0, y0, x1, y1], where x1 >= x0 and y1 >= y0.

  • Start − It represents the starting angle of the arc in degrees. Angles are measured from 3 o’clock, increasing clockwise.

  • end − It represents the ending angle of the arc in degrees.

  • fill − Fill color for the arc line.

  • width − it defines the arc line width in pixels. This parameter was introduced in version 5.3.0.

Examples

Example 1

In this example, an arc is drawn inside a bounding box with specified coordinates and default color and width.

from PIL import Image, ImageDraw

# Create a blank image
image = Image.new("RGB", (300, 300), "black")
draw = ImageDraw.Draw(image)

# Draw an arc inside a bounding box [(50, 50), (250, 250)]
draw.arc([(50, 50), (250, 250)], start=45, end=180)

# Display the image
image.show()
print('Arc is drawn successfully...')

Output

Arc is drawn successfully...

Output Image

imagedraw

Example 2

In this example, a red arc is drawn inside a bounding box with 4 pixels width.

from PIL import Image, ImageDraw
import numpy as np

# Create a NumPy array
arr = np.zeros([300, 700, 3], dtype=np.uint8)
arr[50:250, 50:650] = 250

# Create a Pillow Image from the NumPy array 
image = Image.fromarray(arr)

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

# Draw a red arc inside a bounding box
draw.arc([(100, 70), (450, 240)], start=45, end=180, fill="red", width=4)

# Display the image
image.show()
print('The arc is drawn successfully...')

Output

The arc is drawn successfully...

Output Image

arc_drawn

Example 3

The following example demonstrates how to draw an arc on an existing image with different parameters.

from PIL import Image, ImageDraw
import numpy as np

# Open an Image
image = Image.open('Images/TP-W.png')

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

# Draw a red arc inside a bounding box 
draw.arc([(250, 130), (440, 260)], start=30, end=270, fill="red", width=10)

# Display the image
image.show()

print('The arc is drawn successfully...')

Output

The arc is drawn successfully...
tp logo arc
python_pillow_function_reference.htm
Advertisements