【发布时间】:2020-09-06 08:42:46
【问题描述】:
我有一张 640 x 640 的图像,我想在上面画线。像 PIL 这样的包允许我写一行的常见任务
line(x1,y1,x2,y2)
其中 x1,y1 是起点,x2,y2 是终点线坐标。我有一个不同的任务,我有 x1,y1 和线长(以像素为单位)和方向角。如何仅使用这些数据计算 x2,y2?
谢谢
埃里克
根据 stackoverflow.com 管理员的建议,我正在修改此评论,而不是在下面单独发布答案,以便为我的问题提供代码加上一个疑问,即此代码适用于 0(零)角绘制直线朝向右侧站点,因为它应该是 90度角看起来像更多的 110 或根据图片的东西。我究竟做错了什么 ?再次提前感谢。
import math
from PIL import Image, ImageDraw
def calc_x2y2(length, angle, x1,y1):
x2 = x1 + math.cos(angle)*length
y2 = y1 + math.sin(angle)*length
return (x2,y2)
w, h = 640, 640
x1,y1 = w/2, h/2
shape = [(x1,y1), calc_x2y2(300,90,x1,y1)]
# creating new Image object
img = Image.new("RGB", (w, h),(225,225,225))
# create line on the image
img1 = ImageDraw.Draw(img)
img1.line(shape, fill="#401eba", width=5)
img.show()
【问题讨论】:
-
这能回答你的问题吗? Line Equation with angle