How to get the coordinates of the mouse when it is clicked over an image/labels layer?


is it possible in Napari to get the coordinates of the mouse when it is clicked over an image/labels layers?. In particular, I am interested in implementing a method that would be triggered when I click over an image/labels layers, and inside that method I would use the coordinates of the mouse to extract information about the pixel at that particular locations, e.g., image[x, y], labels[x,y], and then show some additional information on the status bar (is it possible to modify/append the text shown on the status bar?).

I took a look at the napari example " examples/", but it does not run:

AttributeError: ‘Viewer’ object has no attribute ‘mouse_press_callbacks’

Thank you very much for all the help :wink:

Ah! That needs to be updated. We realised that we could implement mouse presses with the same function as mouse drags. So you can replace @viewer.mouse_press_callbacks.append with @viewer.mouse_drag_callbacks.append and it’ll just work! I’ll fix the example shortly. :grimacing:

Additionally, look at the example in the same folder to see how to modify the status bar message.

Thank you very much @jni, I did what you suggested me at everything works fine! ;).
@jni , I’ve just watched a talk about Napari (Interactive Image Processing at Scale, excellent talk!). In this talk, it was mentioned that you implemented a very nice example combining tensorflow/tensorboard and Napari, is it publictly available?..if so, could you please tell me where can I see such example? :slight_smile:

Thank you very much!

1 Like

it’s quite late in australia… so in case we’ve missed @jni for the day, here is his “napariboard” prototype repo:

and here is a tweet from him with a little extra context:


Great!, thank you very much @talley.
By the way, wonderful talk! (Interactive Image Processing at Scale), you and @sofroniewn gave an excellent Napari/Dask introduction! ;), I have learned a lot! :slight_smile:


Hi Huan!
When I try to append a callback function to mouse_drag and print coordinates of event (event.pos), I get the coordinates of a view (or scene?), and not those of a pixel in the image.
Am I doing something wrong? How to get the coordinates of a pixel?


in your callback, you should also have the layer object itself, and the cursor position in data coordinates is at layer.coordinates.

layer = viewer.add_image(...)

def callback(layer, event):
    print(layer.coordinates)  # (0,0) is the center of the upper left pixel
1 Like

Worked like a charm!
thanks a lot for your quick response!