如何使用PIL绘制圆弧?

时间:2017-01-18 20:32:27

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

我得到了如何绘制圆圈,但不是圆弧

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

enter image description here

3 个答案:

答案 0 :(得分:2)

很高兴听到你在评论后取得进展。但是,我并不完全确定你已经得到了它......因为在box=(1, 1, 90, 90)之前不需要draw.arc()声明call - box是一个未使用的变量,对图像或其他任何内容都没有影响,所以不妨删除,因为它会分散注意力并且它的创建只会使事情变得缓慢。

虽然这可能是无关紧要的,但你可能也应该将最外面的(第一个)椭圆绘制得稍微小一些,因此它将完全放置在 90x90像素的图像中(图像中的像素大小将会x和y坐标值从0到89)。你正在做的方式,这个椭圆的最底部和最右边的像素正在被切断。

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

OP's image enlarged

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

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

改进结果:

Image resulting from my changes (enlarged)

答案 1 :(得分:1)

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

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

现在图像非常适合背景中间:

enter image description here

这里有一些关于图像的细节已添加:

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

enter image description here

答案 2 :(得分:0)

正如我在评论中所说,我在上一篇文章中添加了一些代码,还检查了stackoverflow中的其他一些答案:

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /etc/kubernetes/pki/ca.pem
    server: https://X.X.X.33
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: admin
  name: admin@kubernetes
- context:
    cluster: kubernetes
    user: kubelet
  name: kubelet@kubernetes
current-context: admin@kubernetes
kind: Config
users:
- name: admin
  user:
    client-certificate: /etc/kubernetes/pki/admin.pem
    client-key: /etc/kubernetes/pki/admin-key.pem

我明白了:

enter image description here