[英]Draw a line on an image using angle and center point using Python
I'm facing a little issue in my code.我在我的代码中遇到了一个小问题。 I have a dataframe of coords and angels.我有一个坐标和天使的 dataframe。 I want to draw a line from a certain xy coords to the edge of the image on a certain angle (say 45 degree).我想在某个角度(比如 45 度)上从某个 xy 坐标到图像边缘画一条线。
How can I do that using PIL?我怎样才能使用 PIL 做到这一点? Looping over x2 = x + length*cos(angle) doesn't look like a good solution (but I might be wrong here).循环 x2 = x + length*cos(angle) 看起来不是一个好的解决方案(但我在这里可能错了)。
Thanks in advance.提前致谢。
Thanks for posting your solution.感谢您发布您的解决方案。 I've found a good one.我找到了一个很好的。
import math
def get_coords(x, y, angle, imwidth, imheight):
x1_length = (x-imwidth) / math.cos(angle)
y1_length = (y-imheight) / math.sin(angle)
length = max(abs(x1_length), abs(y1_length))
endx1 = x + length * math.cos(math.radians(angle))
endy1 = y + length * math.sin(math.radians(angle))
x2_length = (x-imwidth) / math.cos(angle+180)
y2_length = (y-imheight) / math.sin(angle+180)
length = max(abs(x2_length), abs(y2_length))
endx2 = x + length * math.cos(math.radians(angle+180))
endy2 = y + length * math.sin(math.radians(angle+180))
return endx1, endy1, endx2, endy2
Then I just draw a line between (endx1, endy1) and (endx2, endy2).然后我在 (endx1, endy1) 和 (endx2, endy2) 之间画一条线。
If you have a better solution, I would be very interested to see it.如果您有更好的解决方案,我会非常有兴趣看到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.