2015-10-17 90 views
4

Iam使用PIL,我在圖像上繪製了貝塞爾曲線,我想增加該曲線的厚度。這裏是我的代碼:如何增加PIL中多邊形的厚度ImageDraw

for image in images: 
    img = Image.open("/home/ec2-user/virtualenvs/axonator-production/axonator/media/app_data/ax_picture_20150831_213704.png").convert('RGBA') 
    for annotation in image["annotations"]: 
     xys = [] 
     frame = annotation["frame"].split(",") 
     frame = [int(float(frame[0])),int(float(frame[1])),int(float(frame[2])),int(float(frame[3]))] 
     frame_location = (frame[0],frame[1]) 
     frame_size = (5000 , 5000) 
     for point in annotation["path"]: 
      pt = point["points"].split(",") 
      xys.append((pt[0],pt[1])) 
     bezier = make_bezier(xys) 
     points = bezier(ts) 
     curve = Image.new('RGBA', frame_size) 
     import pdb; pdb.set_trace() 
     curve_draw = ImageDraw.Draw(curve) 
     curve_draw.polygon(points,outline="red") 
     curve_draw.text(points[0],str(order)) 
     order = order + 1 
     img.paste(curve,frame_location,mask = curve) 
    img.save('out.png') 
+0

你可能要添加一個明確的'curve_draw.line(點,寬度= 9)'後的多邊形。 – meuh

回答

4

功能draw.polygon()不能採取「寬」的說法一樣line()即可。

除此之外,line()將採取一系列的點,並將繪製一條折線。

線條的結尾會很醜,但通過在結尾繪製圓圈,您可以讓它們變得漂亮!

下面的代碼繪製了一個美麗的厚紅色多邊形。

enter image description here

from PIL import Image, ImageDraw 

points = (
    (30, 40), 
    (120, 60), 
    (110, 90), 
    (20, 110), 
    (30, 40), 
    ) 

im = Image.new("RGB", (130, 120)) 
dr = ImageDraw.Draw(im) 
dr.line(points, fill="red", width=9) 
for point in points: 
    dr.ellipse((point[0] - 4, point[1] - 4, point[0] + 4, point[1] + 4), fill="red") 
im.save("polygon.png") 
+0

真棒........,它工作。謝謝Michiel :-) –