【问题标题】:How can I draw an arc using PIL?如何使用 PIL 绘制圆弧?
【发布时间】:2017-01-18 20:32:27
【问题描述】:

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

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()

【问题讨论】:

标签: python automatic-ref-counting python-imaging-library geometry


【解决方案1】:

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

虽然它可能无关紧要,但您可能还应该使绘制的最外层(第一个)椭圆稍微小一点,这样它将完全 90x90 像素的图像中(图像中的像素大小将具有从 0 到 89 的 x 和 y 坐标值)。按照您的操作方式,这个椭圆最底部和最右侧的像素会被截断。

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


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

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()

改进的结果:

【讨论】:

  • 谢谢,你的建议很棒!
【解决方案2】:

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

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()

【讨论】:

  • 看起来你已经掌握了窍门(学会了正确的做法)。请考虑接受我的回答,因为它和我在你的问题下的原始评论是让你达到你现在的观点的原因。
  • 谢谢你,Martineau,我还在图片中添加了一些文字,我会在这里添加它,也许,因为我也在这里发布了它们:my python blog
【解决方案3】:

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

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")

我明白了:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多