Python Pillow - ImageDraw.chord() 函式



ImageDraw.chord() 方法用於繪製弦(圓的一部分),該弦位於由兩點定義的邊界框內(從起始角度繪製到結束角度)。它與 arc() 方法相同,但兩端點之間連線一條直線。圓的弦是連線圓周上兩點的直線段。

語法

以下是函式的語法:

ImageDraw.chord(xy, start, end, fill=None, outline=None, width=1)

引數

以下是此函式引數的詳細資訊:

  • xy - 定義弦的邊界框的兩點。可以將其指定為兩個元組的序列 [(x0, y0), (x1, y1)],也可以指定為扁平列表 [x0, y0, x1, y1]。無論哪種情況,都必須滿足條件 x1 >= x0 且 y1 >= y0。

  • start - 弦的起始角度,以度為單位。角度從 3 點鐘方向開始測量,順時針方向遞增。

  • end - 弦的結束角度,也以度為單位。

  • fill - 用於填充弦的顏色。

  • outline - 用於弦輪廓的顏色。

  • width - 弦輪廓的線寬,以畫素為單位。預設值為 1。

示例

示例 1

此示例在指定的邊界框內繪製弦,使用預設填充顏色、輪廓和寬度。

from PIL import Image, ImageDraw

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

# Draw a chord inside a bounding box [(100, 10), (350, 250)]
draw.chord([(100, 10), (350, 250)], start=45, end=180)

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

輸出

Chord is drawn successfully...

輸出影像

bounding box

示例 2

此示例在指定的邊界框內繪製弦,填充顏色為藍色,輪廓為黑色,輪廓線寬為 2 畫素。

from PIL import Image, ImageDraw

# Create a new image with a white background
image = Image.new("RGB", (700, 300), "white")
draw = ImageDraw.Draw(image)

# Draw a chord inside the bounding box
draw.chord([(100, 10), (350, 250)], start=30, end=300, fill="blue", outline="black", width=2)

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

輸出

The Chord is drawn successfully...

輸出影像

blue chord

示例 3

以下示例演示如何在現有影像上使用不同的引數繪製弦。

from PIL import Image, ImageDraw

# Open an Image
image = Image.open('Images/ColorDots.png')

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

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

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

輸出

The chord is drawn successfully...

輸出影像

red chord
python_pillow_function_reference.htm
廣告