I am trying to do my assignment which ask me to draw a line like thisenter image description here
the center point perpendicular to diagonal and the code is below
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
image = Image.open(image_path)
image_width, image_height = image.size
k1 = image_height / image_width
b1 = 0
k2 = -k1
b2 = image_height
x1 = (max_center[1] - b1) / k1
x2 = (max_center[1] - b2) / k2
y1 = k1 * x1 + b1
y2 = k2 * x2 + b2
plt.plot([0, image_width], [b1, k1 * image_width + b1], 'r')
plt.plot([0, image_width], [b2, k2 * image_width + b2], 'r')
plt.plot([max_center[0] * image_width, x1], [max_center[1] * image_height, y1], 'r')
plt.plot([max_center[0] * image_width, x2], [max_center[1] * image_height, y2], 'r')
plt.imshow(image)
plt.show()
and when I run the code it gives me the picture like this enter image description here
how should I achieve it
Your lines
plt.plot([max_center[0] * image_width, x1], [max_center[1] * image_height, y1], 'r')
plt.plot([max_center[0] * image_width, x2], [max_center[1] * image_height, y2], 'r')
don't make any sense to me. You are multiplying coordinates of a point with the image dimensions.
I'll try to guide you through the necessary caculations. For this example I used an arbitrary point at 50, 150, but it will work for any other point as well.
For calculating the slope k_perp
of a line perpendicular to one with slope k
I used:
k_perp * k = -1
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
image = Image.open(image_path)
image_width, image_height = image.size
k1 = image_height / image_width
b1 = 0
k2 = -k1
b2 = image_height
plt.plot([0, image_width], [b1, k1 * image_width + b1], 'r')
plt.plot([0, image_width], [b2, k2 * image_width + b2], 'r')
# definition of point
point_x = 50
point_y = 150
plt.plot(point_x, point_y, 'r.', label='Point')
# line perpendicular to diagonal 1 and containing point
k_perp = -1/k1
b_perp = point_y - point_x * k_perp
# calculating foot point for diagonal 1
x = (b_perp - b1) / (k1 - k_perp)
y = k1 * x + b1
plt.plot([point_x, x],[point_y, y], 'g', label='Perpendicular line 1')
# line perpendicular to diagonal 2 and containing point
k_perp = -1/k2
b_perp = point_y - point_x * k_perp
# calculating foot point for diagonal 2
x = (b_perp - b2) / (k2 - k_perp)
y = k2 * x + b2
plt.plot([point_x, x],[point_y, y], 'g', label='Perpendicular line 2')
plt.imshow(image)
plt.legend()
plt.show()
From this, calculating the distance between the point to the diagonals should be straight forward.