Home > OS >  how to get mouse location once but dont return and until I get the mouse location twice in napari us
how to get mouse location once but dont return and until I get the mouse location twice in napari us

Time:11-21

I am writing on a napari plugin. I have the following to retrieve mouse location

img = cv2.imread("../medium/24708.1_4 at 20X.jpg", cv2.IMREAD_COLOR)
viewer = napari.view_image(img)
layer = viewer
@layer.mouse_drag_callbacks.append
def callback(layer, event):  # (0,0) is the center of the upper left pixel
     x,y = viewer.cursor.position
     print(x,y)

but this will print the mouse location immediately after I click. I want to get the mouse location once I click but dont print it until I click the mouse again, which means I want to print the mouse position twice at once. I tried loops, but it just return same mouse location.

@layer.mouse_drag_callbacks.append
def callback(layer, event):  # (0,0) is the center of the upper left pixel
i=1
arr=[]
while i<=2:
    x,y = viewer.cursor.position
    x = round(x)
    y = round(y)
    arr.append(x)
    arr.append(y)
    i  = 1
print(arr)
[1376, 691, 1376, 691]
[1506, 1117, 1506, 1117]
[1575, 826, 1575, 826]

CodePudding user response:

I don't know what you are going to do with the points, or what how you are going to decide how many are needed, but you will need to collect the points. A list seems to fit the bill:

points = []

@layer.mouse_drag_callbacks.append
def callback(layer, event):  # (0,0) is the center of the upper left pixel
     points.append(viewer.cursor.position)
  • Related