使用 Python 使用角度和中心点在图像上画一条线

Draw a line on an image using angle and center point using Python

我的代码遇到了一个小问题。我有一个坐标和天使的数据框。 我想从某个 xy 坐标以某个角度(比如 45 度)画一条线到图像的边缘。

我如何使用 PIL 做到这一点?循环 x2 = x + length*cos(angle) 看起来不是一个好的解决方案(但我在这里可能是错的)。

提前致谢。

感谢您发布您的解决方案。我找到了一个好人。

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

然后我就在 (endx1, endy1) 和 (endx2, endy2) 之间画一条线。

如果你有更好的解决方案,我很想看看