我正在使用PIL在我的项目中绘制Bezier曲线。我有绘制贝塞尔曲线的路径点。曲线正在绘制正确。但我想在图像上的特定帧上绘制贝塞尔曲线。我有以下数据:
{"frame":"89.062515,99.718819,175.781235,105.468742","path":
[{119.531258,4.687508,0.000000,0.000000},
{119.531258,4.687508,111.328133,4.687508},.......]}
我想在所属框架中显示路径。
这是我目前的代码:
images = [{"annotations":[{"notes":"Problem here\\n","frame":"89.062515,99.718819,175.781235,105.468742","path":[{"points":"119.531258,4.687508,0.000000,0.000000","type":"m"},{"points":"119.531258,4.687508,111.328133,4.687508","type":"q"},{"points":"103.125008,4.687508,103.125008,4.687508","type":"q"},{"points":"103.125008,4.687508,92.578133,4.687508","type":"q"},{"points":"82.031258,4.687508,76.171883,3.515633","type":"q"},{"points":"70.312508,2.343758,63.281258,2.343758","type":"q"},{"points":"56.250008,2.343758,44.531258,2.343758","type":"q"},{"points":"32.812508,2.343758,29.296883,2.343758","type":"q"},{"points":"25.781258,2.343758,16.406258,3.515633","type":"q"},{"points":"7.031258,4.687508,3.515633,10.546883","type":"q"},{"points":"0.000008,16.406258,0.000008,24.609390","type":"q"},{"points":"0.000008,32.812515,3.515633,38.671890","type":"q"},{"points":"7.031258,44.531265,15.234383,52.734390","type":"q"},{"points":"23.437508,60.937515,31.640633,71.484390","type":"q"},{"points":"39.843758,82.031265,45.703133,89.062500","type":"q"},{"points":"51.562508,96.093750,57.421883,99.609375","type":"q"},{"points":"63.281258,103.125000,64.453133,103.125000","type":"q"},{"points":"65.625008,103.125000,70.312508,104.296875","type":"q"},{"points":"75.000008,105.468750,84.375008,105.468750","type":"q"},{"points":"93.750008,105.468750,103.125008,105.468750","type":"q"},{"points":"112.500008,105.468750,128.906250,98.437500","type":"q"},{"points":"145.312500,91.406250,150.000000,87.890625","type":"q"},{"points":"154.687500,84.375015,161.718750,78.515640","type":"q"},{"points":"168.750000,72.656265,172.265625,70.312515","type":"q"},{"points":"175.781250,67.968765,175.781250,65.625015","type":"q"},{"points":"175.781250,63.281265,175.781250,59.765640","type":"q"},{"points":"175.781250,56.250015,175.781250,51.562515","type":"q"},{"points":"175.781250,46.875015,175.781250,45.703140","type":"q"},{"points":"175.781250,44.531265,173.437500,43.359390","type":"q"},{"points":"171.093750,42.187515,169.921875,41.015640","type":"q"},{"points":"168.750000,39.843765,167.578125,38.671890","type":"q"},{"points":"166.406250,37.500015,161.718750,35.156265","type":"q"},{"points":"157.031250,32.812515,152.343750,32.812515","type":"q"},{"points":"147.656250,32.812515,144.140625,31.640640","type":"q"},{"points":"140.625000,30.468765,135.937500,30.468765","type":"q"},{"points":"131.250000,30.468765,127.734383,28.125015","type":"q"},{"points":"124.218758,25.781258,123.046883,25.781258","type":"q"},{"points":"121.875008,25.781258,119.531258,24.609383","type":"q"},{"points":"117.187508,23.437508,114.843758,21.093758","type":"q"},{"points":"112.500008,18.750008,111.328133,18.750008","type":"q"},{"points":"110.156258,18.750008,107.812508,12.890633","type":"q"},{"points":"105.468758,7.031258,105.468758,4.687508","type":"q"},{"points":"105.468758,2.343758,105.468758,1.171883","type":"q"},{"points":"105.468758,0.000008,106.640633,0.000008","type":"q"}]},{"notes":"Isent it really awesome","frame":"663.281250,153.625076,433.593750,498.437500","path":[{"points":"400.781250,9.375015,0.000000,0.000000","type":"m"},{"points":"400.781250,9.375015,389.062500,8.203140","type":"q"},{"points":"377.343750,7.031265,377.343750,7.031265","type":"q"},{"points":"377.343750,7.031265,367.968750,3.515640","type":"q"},{"points":"358.593750,0.000015,323.437500,0.000015","type":"q"},{"points":"288.281250,0.000015,253.125000,0.000015","type":"q"},{"points":"217.968750,0.000015,215.625000,0.000015","type":"q"},{"points":"213.281250,0.000015,206.250000,0.000015","type":"q"},{"points":"199.218750,0.000015,198.046875,0.000015","type":"q"},{"points":"196.875000,0.000015,193.359375,0.000015","type":"q"},{"points":"189.843750,0.000015,188.671875,0.000015","type":"q"},{"points":"187.500000,0.000015,182.812500,3.515640","type":"q"},{"points":"178.125000,7.031265,171.093750,8.203140","type":"q"},{"points":"164.062500,9.375015,160.546875,10.546890","type":"q"},{"points":"157.031250,11.718765,148.828125,18.750015","type":"q"},{"points":"140.625000,25.781265,131.250000,33.984375","type":"q"},{"points":"121.875000,42.187500,110.156250,53.906250","type":"q"},{"points":"98.437500,65.625000,87.890625,78.515625","type":"q"},{"points":"77.343750,91.406250,66.796875,104.296875","type":"q"},{"points":"56.250000,117.187500,43.359375,138.281250","type":"q"},{"points":"30.468750,159.375000,26.953125,172.265625","type":"q"},{"points":"23.437500,185.156250,19.921875,191.015625","type":"q"},{"points":"16.406250,196.875000,14.062500,201.562500","type":"q"},{"points":"11.718750,206.250000,10.546875,210.937500","type":"q"},{"points":"9.375000,215.625000,8.203125,226.171875","type":"q"},{"points":"7.031250,236.718750,4.687500,241.406250","type":"q"},{"points":"2.343750,246.093750,1.171875,260.156250","type":"q"},{"points":"0.000000,274.218750,0.000000,281.250000","type":"q"},{"points":"0.000000,288.281250,0.000000,297.656250","type":"q"},{"points":"0.000000,307.031250,3.515625,315.234375","type":"q"},{"points":"7.031250,323.437500,16.406250,335.156250","type":"q"},{"points":"25.781250,346.875000,35.156250,359.765625","type":"q"},{"points":"44.531250,372.656250,52.734375,383.203125","type":"q"},{"points":"60.937500,393.750000,73.828125,404.296875","type":"q"},{"points":"86.718750,414.843750,100.781250,426.562500","type":"q"},{"points":"114.843750,438.281250,131.250000,451.171875","type":"q"},{"points":"147.656250,464.062500,167.578125,475.781250","type":"q"},{"points":"187.500000,487.500000,199.218750,491.015625","type":"q"},{"points":"210.937500,494.531250,217.968750,495.703125","type":"q"},{"points":"225.000000,496.875000,236.718750,498.046875","type":"q"},{"points":"248.437500,499.218750,261.328125,496.875000","type":"q"},{"points":"274.218750,494.531250,288.281250,486.328125","type":"q"},{"points":"302.343750,478.125000,321.093750,465.234375","type":"q"},{"points":"339.843750,452.343750,351.562500,439.453125","type":"q"},{"points":"363.281250,426.562500,377.343750,406.640625","type":"q"},{"points":"391.406250,386.718750,394.921875,377.343750","type":"q"},{"points":"398.437500,367.968750,403.125000,359.765625","type":"q"},{"points":"407.812500,351.562500,413.671875,337.500000","type":"q"},{"points":"419.531250,323.437500,423.046875,314.062500","type":"q"},{"points":"426.562500,304.687500,430.078125,294.140625","type":"q"},{"points":"433.593750,283.593750,433.593750,267.187500","type":"q"},{"points":"433.593750,250.781250,433.593750,237.890625","type":"q"},{"points":"433.593750,225.000000,433.593750,216.796875","type":"q"},{"points":"433.593750,208.593750,433.593750,203.906250","type":"q"},{"points":"433.593750,199.218750,431.250000,195.703125","type":"q"},{"points":"428.906250,192.187500,427.734375,187.500000","type":"q"},{"points":"426.562500,182.812500,426.562500,179.296875","type":"q"},{"points":"426.562500,175.781250,426.562500,167.578125","type":"q"},{"points":"426.562500,159.375000,426.562500,158.203125","type":"q"},{"points":"426.562500,157.031250,426.562500,152.343750","type":"q"},{"points":"426.562500,147.656250,426.562500,141.796875","type":"q"},{"points":"426.562500,135.937500,426.562500,130.078125","type":"q"},{"points":"426.562500,124.218750,426.562500,118.359375","type":"q"},{"points":"426.562500,112.500000,426.562500,107.812500","type":"q"},{"points":"426.562500,103.125000,426.562500,98.437500","type":"q"},{"points":"426.562500,93.750000,426.562500,86.718750","type":"q"},{"points":"426.562500,79.687500,426.562500,73.828125","type":"q"},{"points":"426.562500,67.968750,426.562500,63.281250","type":"q"},{"points":"426.562500,58.593750,426.562500,57.421875","type":"q"},{"points":"426.562500,56.250000,426.562500,53.906250","type":"q"},{"points":"426.562500,51.562500,426.562500,46.875000","type":"q"},{"points":"426.562500,42.187500,426.562500,37.500000","type":"q"},{"points":"426.562500,32.812500,426.562500,30.468750","type":"q"},{"points":"426.562500,28.125015,426.562500,26.953140","type":"q"},{"points":"426.562500,25.781265,426.562500,24.609390","type":"q"},{"points":"426.562500,23.437515,424.218750,21.093765","type":"q"},{"points":"421.875000,18.750015,421.875000,17.578140","type":"q"},{"points":"421.875000,16.406265,419.531250,16.406265","type":"q"},{"points":"417.187500,16.406265,417.187500,15.234390","type":"q"},{"points":"417.187500,14.062515,416.015625,14.062515","type":"q"},{"points":"414.843750,14.062515,413.671875,14.062515","type":"q"},{"points":"412.500000,14.062515,411.328125,14.062515","type":"q"},{"points":"410.156250,14.062515,408.984375,14.062515","type":"q"},{"points":"407.812500,14.062515,406.640625,14.062515","type":"q"},{"points":"405.468750,14.062515,404.296875,14.062515","type":"q"},{"points":"403.125000,14.062515,401.953125,14.062515","type":"q"},{"points":"400.781250,14.062515,398.437500,14.062515","type":"q"},{"points":"396.093750,14.062515,394.921875,14.062515","type":"q"},{"points":"393.750000,14.062515,391.406250,14.062515","type":"q"},{"points":"389.062500,14.062515,389.062500,11.718765","type":"q"},{"points":"389.062500,9.375015,387.890625,9.375015","type":"q"},{"points":"386.718750,9.375015,384.375000,8.203140","type":"q"}]},{"notes":"asdkasdasd","frame":"182.812515,624.718811,292.968750,234.375000","path":[{"points":"290.625000,98.437500,0.000000,0.000000","type":"m"},{"points":"290.625000,98.437500,289.453125,94.921875","type":"q"},{"points":"288.281250,91.406250,288.281250,91.406250","type":"q"},{"points":"288.281250,91.406250,284.765625,85.546875","type":"q"},{"points":"281.250000,79.687500,277.734375,76.171875","type":"q"},{"points":"274.218750,72.656250,268.359375,63.281250","type":"q"},{"points":"262.500000,53.906250,255.468750,49.218750","type":"q"},{"points":"248.437500,44.531250,236.718750,36.328125","type":"q"},{"points":"225.000000,28.125000,215.625000,23.437500","type":"q"},{"points":"206.250000,18.750000,196.875000,16.406250","type":"q"},{"points":"187.500000,14.062500,179.296875,10.546875","type":"q"},{"points":"171.093750,7.031250,162.890625,4.687500","type":"q"},{"points":"154.687500,2.343750,150.000000,2.343750","type":"q"},{"points":"145.312500,2.343750,137.109375,1.171875","type":"q"},{"points":"128.906250,0.000000,120.703125,0.000000","type":"q"},{"points":"112.500000,0.000000,99.609375,0.000000","type":"q"},{"points":"86.718750,0.000000,78.515625,0.000000","type":"q"},{"points":"70.312515,0.000000,62.109390,0.000000","type":"q"},{"points":"53.906265,0.000000,49.218765,1.171875","type":"q"},{"points":"44.531265,2.343750,41.015640,7.031250","type":"q"},{"points":"37.500015,11.718750,33.984390,19.921875","type":"q"},{"points":"30.468765,28.125000,25.781265,32.812500","type":"q"},{"points":"21.093765,37.500000,17.578140,39.843750","type":"q"},{"points":"14.062515,42.187500,12.890640,46.875000","type":"q"},{"points":"11.718765,51.562500,9.375015,57.421875","type":"q"},{"points":"7.031265,63.281250,3.515640,70.312500","type":"q"},{"points":"0.000015,77.343750,0.000015,82.031250","type":"q"},{"points":"0.000015,86.718750,0.000015,92.578125","type":"q"},{"points":"0.000015,98.437500,0.000015,107.812500","type":"q"},{"points":"0.000015,117.187500,0.000015,124.218750","type":"q"},{"points":"0.000015,131.250000,0.000015,137.109375","type":"q"},{"points":"0.000015,142.968750,2.343765,147.656250","type":"q"},{"points":"4.687515,152.343750,7.031265,159.375000","type":"q"},{"points":"9.375015,166.406250,14.062515,171.093750","type":"q"},{"points":"18.750015,175.781250,22.265640,179.296875","type":"q"},{"points":"25.781265,182.812500,29.296890,186.328125","type":"q"},{"points":"32.812515,189.843750,39.843765,194.531250","type":"q"},{"points":"46.875015,199.218750,51.562515,202.734375","type":"q"},{"points":"56.250015,206.250000,62.109390,208.593750","type":"q"},{"points":"67.968765,210.937500,72.656250,213.281250","type":"q"},{"points":"77.343750,215.625000,86.718750,217.968750","type":"q"},{"points":"96.093750,220.312500,100.781250,223.828125","type":"q"},{"points":"105.468750,227.343750,112.500000,229.687500","type":"q"},{"points":"119.531250,232.031250,131.250000,233.203125","type":"q"},{"points":"142.968750,234.375000,152.343750,234.375000","type":"q"},{"points":"161.718750,234.375000,178.125000,234.375000","type":"q"},{"points":"194.531250,234.375000,206.250000,232.031250","type":"q"},{"points":"217.968750,229.687500,222.656250,227.343750","type":"q"},{"points":"227.343750,225.000000,230.859375,220.312500","type":"q"},{"points":"234.375000,215.625000,237.890625,208.593750","type":"q"},{"points":"241.406250,201.562500,244.921875,200.390625","type":"q"},{"points":"248.437500,199.218750,251.953125,194.531250","type":"q"},{"points":"255.468750,189.843750,256.640625,183.984375","type":"q"},{"points":"257.812500,178.125000,260.156250,176.953125","type":"q"},{"points":"262.500000,175.781250,264.843750,172.265625","type":"q"},{"points":"267.187500,168.750000,268.359375,165.234375","type":"q"},{"points":"269.531250,161.718750,271.875000,160.546875","type":"q"},{"points":"274.218750,159.375000,275.390625,155.859375","type":"q"},{"points":"276.562500,152.343750,277.734375,148.828125","type":"q"},{"points":"278.906250,145.312500,281.250000,142.968750","type":"q"},{"points":"283.593750,140.625000,283.593750,139.453125","type":"q"},{"points":"283.593750,138.281250,284.765625,138.281250","type":"q"},{"points":"285.937500,138.281250,287.109375,134.765625","type":"q"},{"points":"288.281250,131.250000,290.625000,130.078125","type":"q"},{"points":"292.968750,128.906250,292.968750,126.562500","type":"q"},{"points":"292.968750,124.218750,292.968750,123.046875","type":"q"},{"points":"292.968750,121.875000,292.968750,119.531250","type":"q"},{"points":"292.968750,117.187500,292.968750,116.015625","type":"q"},{"points":"292.968750,114.843750,290.625000,114.843750","type":"q"},{"points":"288.281250,114.843750,288.281250,112.500000","type":"q"},{"points":"288.281250,110.156250,288.281250,108.984375","type":"q"},{"points":"288.281250,107.812500,287.109375,107.812500","type":"q"},{"points":"285.937500,107.812500,285.937500,106.640625","type":"q"},{"points":"285.937500,105.468750,285.937500,104.296875","type":"q"},{"points":"285.937500,103.125000,285.937500,100.781250","type":"q"},{"points":"285.937500,98.437500,285.937500,97.265625","type":"q"},{"points":"285.937500,96.093750,285.937500,94.921875","type":"q"}]}],"name":"AX_User_4_Image_16_October_2015_20_14_19.395_pm.jpeg","long":0,"date":"16-10-2015 20:14:19.402 pm","lat":0,"mediaType":0,"image":"AX_User_4_Image_16_October_2015_20_14_19.395_pm.jpeg"}]
for image in images:
img = Image.open("/Users/priya/Gaurav/Axonator/media/app_data/AX_User_4_Image_16_October_2015_20_14_19.395_pm.jpeg").convert('RGBA')
draw = ImageDraw.Draw(img)
for annotation in image["annotations"]:
xys = []
frame = annotation["frame"].split(",")
for point in annotation["path"]:
pt = point["points"].split(",")
xys.append((pt[0],pt[1]))
bezier = make_bezier(xys)
points = bezier(ts)
print points
draw.polygon(points,outline="red")
img.save('out.png')
def make_bezier(xys):
# xys should be a sequence of 2-tuples (Bezier control points)
n = len(xys)
combinations = pascal_row(n-1)
def bezier(ts):
# This uses the generalized formula for bezier curves
# http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Generalization
result = []
for t in ts:
tpowers = (t**i for i in range(n))
upowers = reversed([(1-t)**i for i in range(n)])
coefs = [c*a*b for c, a, b in zip(combinations, tpowers, upowers)]
result.append(
tuple(sum([float(coef)*float(p) for coef, p in zip(coefs, ps)]) for ps in zip(*xys)))
return result
return bezier
def pascal_row(n):
# This returns the nth row of Pascal's Triangle
result = [1]
x, numerator = 1, n
for denominator in range(1, n//2+1):
# print(numerator,denominator,x)
x *= numerator
x /= denominator
result.append(x)
numerator -= 1
if n&1 == 0:
# n is even
result.extend(reversed(result[:-1]))
else:
result.extend(reversed(result))
return result