Python 通过图像的两点绘制一条线

Python 通过图像的两点绘制一条线,python,image,matplotlib,plot,Python,Image,Matplotlib,Plot,我有一个图像,我正在加载到一个二维数组使用pyfits。我想在图像上画一条穿过两个像素的线,并用添加的新线(而不是绘图)保存它。在此之后,我想画一条线垂直于前一条线与不同的颜色。使用matplotlib实现此目的的最佳方法是什么?我用PIL试过了。我没能做到。请看代码,并建议我一种方法来做到这一点。我附加的形象也 def plotAxes(map, angle, x_centroid, y_centroid): hor = math.floor(x_centroid + 20*(math

我有一个图像,我正在加载到一个二维数组使用pyfits。我想在图像上画一条穿过两个像素的线,并用添加的新线(而不是绘图)保存它。在此之后,我想画一条线垂直于前一条线与不同的颜色。使用matplotlib实现此目的的最佳方法是什么?我用PIL试过了。我没能做到。请看代码,并建议我一种方法来做到这一点。我附加的形象也

def plotAxes(map, angle, x_centroid, y_centroid):
    hor = math.floor(x_centroid + 20*(math.cos(angle)))
    ver = math.floor(y_centroid - 20*(math.sin(angle)))
    hor1 = math.floor(x_centroid + 20*(math.cos(angle+90.0)))
    ver1 = math.floor(y_centroid - 20*(math.sin(angle+90.0)))
    map_height = len(map)
    map_width = len(map[0])
    point = [ver, hor]
    center = [y_centroid, x_centroid]
    Max = np.max(map)
    array = np.zeros((map_height, map_width), int)
    for i in range(0, map_height):
        for j in range(0, map_width):
            array[i][j] = (math.floor((float(map[i][j])/float(Max))*255))
    im = Image.fromarray(np.uint8(array))
    draw = ImageDraw.Draw(im) 
    draw.line((x_centroid,y_centroid, hor,ver  ), fill="red")
    draw.line((x_centroid,y_centroid, hor1,ver1  ), fill="red")
    im.show()
但是上面的代码似乎没有垂直打印行。角度看起来是120而不是90


对不起。我犯了一个错误,将角度以度表示为正反方向。我通过了弧度测试,它成功了。谢谢

angle = (angle * math.pi)/180
hor = math.floor(x_centroid + 20*(math.cos(angle)))
ver = math.floor(y_centroid - 20*(math.sin(angle)))
hor1 = math.floor(x_centroid + 20*(math.cos(angle+ (math.pi)/2)))
ver1 = math.floor(y_centroid - 20*(math.sin(angle+(math.pi)/2)))

这个编辑确实改进了这个问题;投票重新开放。我做了必要的修改。抱歉之前没有说得更清楚:)。谢谢