import cv2
def mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
img1 = img.copy()
xy = "(%d,%d)" % (x, y) # 设置坐标显示格式
cv2.circle(img1, (x, y), 1, (0, 255, 0), thickness=-1)
cv2.putText(img1, xy, (x+10, y-10), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), thickness=1)
cv2.imshow("image", img1) # 显示坐标
def get_coordinate_by_click(img_path):
global img
img = cv2.imread(img_path) # 图片路径
cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE) # 设置窗口标题和大小
# cv2.resizeWindow('image', 1000, 400)
cv2.setMouseCallback("image", mouse)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__=='__main__':
img_path = "E:\import\\1.png"
get_coordinate_by_click(img_path)
运行结果: