如何使用 PIL 绘制圆弧?

How can I draw an arc using PIL?

我知道如何画圆,但不会画弧

from PIL import Image, ImageDraw

img = Image.new("RGB",(60,60),'white')
dr = ImageDraw.Draw(img)
dr.ellipse((0,0,60,60),'yellow','black')

现在我明白了,谢谢你的帮助..

from PIL import Image
from PIL import ImageDraw

image = Image.new('RGB',(90,90),'white')
draw = ImageDraw.Draw(image)
draw.ellipse((0,0,90,90),'yellow','blue')
draw.ellipse((25,20,35,30),'yellow','blue')
draw.ellipse((50,20,60,30),'yellow','blue')
draw.arc((20,40,70,70), 0, 180, 0) #draw circle in black
image.show()

很高兴听到您在我发表评论后取得了进步。但是我不完全确定你有 "got it"...因为在 draw.arc() 调用之前不需要那个 box=(1, 1, 90, 90) 语句——box 是一个未使用的变量对图像或其他任何东西都没有影响,所以也可以删除,因为它会分散注意力,而且它的创建只会稍微减慢速度。

虽然它可能无关紧要,但您可能还应该将最外层(第一个)椭圆画得稍微小一点,这样它就会完全 90x90 像素图像(像素在该大小的图像将具有从 0 到 89 的 x 和 y 坐标值)。你这样做的方式,这个椭圆的 bottom-most 和最右边的像素得到 cut-off.

为了说明我的意思,这是您问题末尾的代码生成的图像的(放大)版本:


下面是应用了这些小修正的相同代码,下面是它生成的略有不同的图像:

from PIL import Image
from PIL import ImageDraw

image = Image.new('RGB', (90, 90), 'white')
draw = ImageDraw.Draw(image)
draw.ellipse((0, 0, 89, 89), 'yellow', 'blue')  # made this a little smaller..
draw.ellipse((25, 20, 35, 30), 'yellow', 'blue')
draw.ellipse((50, 20, 60, 30), 'yellow', 'blue')
draw.arc((20, 40, 70, 70), 0, 180, 'black')  # draw an arc in black
image.show()

改进的结果:

非常感谢,所以我根据您的完美建议做了一些修改:

from PIL import Image
from PIL import ImageDraw

image = Image.new('RGB',(91,91),'white') # background image
draw = ImageDraw.Draw(image) # this makes you draw on image
draw.ellipse((0,0,90,90),'yellow','blue') # face
draw.ellipse((25,20,35,30),'yellow','blue') # left eye
draw.ellipse((50,20,60,30),'yellow','blue') # right eye
draw.arc((20,40,70,70), 0, 180, 0) # smile
image.show()

现在图像完全适合背景的中间:

这里添加了一些关于图片的细节:

from PIL import Image
from PIL import ImageDraw

image = Image.new('RGB',(91,91),'blue')
draw = ImageDraw.Draw(image)
draw.ellipse((0,0,90,90),'yellow','blue') # face
draw.ellipse((25,20,35,30),'yellow','blue') # left eye
draw.ellipse((28,25,32,30),'blue','blue') # left ...
draw.ellipse((50,20,60,30),'yellow','blue') # right eye
draw.ellipse((53,25,58,30),'blue','blue') # right ...
draw.arc((40,50,50,55), 0, 360, 0) # nose
draw.arc((20,40,70,70), 0, 180, 0) # smile
image.show()

正如我在评论中所说,我在之前的代码中添加了一些代码,还在 Whosebug 中检查了一些其他答案:

from PIL import Image,ImageDraw,ImageFont

# sample text and font
unicode_text = u"SMILE WORLD!" # This is used to shape the image around the text size
font = ImageFont.truetype("consola.ttf", 28, encoding="unic")
font2 = ImageFont.truetype("consola.ttf", 10, encoding="unic")
# get the line size
text_width, text_height = font.getsize(unicode_text)

# create a blank canvas with extra space between lines
# I added 100 to host the image and 30 for the watermark length
canvas = Image.new('RGB', (text_width + 30, text_height + 100), "orange")

# draw the text onto the text canvas, and use black as the text color
draw = ImageDraw.Draw(canvas)
draw.text((5,5), unicode_text, 'blue', font) # This paste the unicode_text=u"SMILE WORLD"

def smile(x=30,y=30):
    draw = ImageDraw.Draw(canvas)
    draw.ellipse((0+x,0+y,90+x,90+y),'yellow','blue') # face
    draw.ellipse((25+x,20+y,35+x,30+y),'yellow','blue') # left eye
    draw.ellipse((28+x,25+y,32+x,30+y),'blue','blue') # left ...
    draw.ellipse((50+x,20+y,60+x,30+y),'yellow','blue') # right eye
    draw.ellipse((53+x,25+y,58+x,30+y),'blue','blue') # right ...
    draw.arc((40+x,50+y,50+x,55+y), 0, 360, 0) # nose
    draw.arc((20+x,40+y,70+x,70+y), 0, 180, 0) # smile

smile(60,28)    
draw.text((5,100), u'This image is covered by copyright', 'blue', font2)
draw.text((5,110), u'pythonprogramming.altervista.org', 'blue', font2)
canvas.show()
canvas.save("smile.png")

我明白了: