简体   繁体   English

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

[英]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.

相关问题 如何在python中使用matplotlib.plyplot赋予id指向点并绘制线到点 - How to give id to point and draw line point to point using matplotlib.plyplot in python 使用PIL将文本绘制到图像的中心 - draw text to center of the image using PIL 如何使用起点、长度和角度在 Shapely 中创建一条线 - How to create a line in Shapely using starting point, length, and an angle 如何通过在 Python 中给出终点和相对于垂直轴的角度在图像上绘制线条 - How to draw lines on an image by giving the end point and an angle with respect to the vertical axis in Python 如何使用python的PIL以一定角度绘制文本? - How do I draw text at an angle using python's PIL? 如何使用给定的坐标在图像中绘制一个点 - How to draw a point in an image using given coordinate 使用 OpenCV 的直线角度 - Angle of a line using OpenCV OpenCV python:如何使用渐变和第一个点画一条线? - OpenCV python: How do I draw a line using the gradient and the first point? 如何使用 python-pptx 从形状的连接点到另一个形状的连接点画一条线? - How can I draw a line from shape's connecting point to another shape's connecting point using python-pptx? 线条不会使用ImageDraw在图像上绘制 - Line won't draw on an image using ImageDraw
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM