The best approach to interact with images by clicking on them and retreiving position of the pixel I clicked on

I’m loading an image via imshow() which allows me to track coordinates of the mouse as I hover it over the image. I’m using the coordinates as inputs for some other stuff.

I wonder if it’s possible instead to display an image, click somewhere on it, and have some code pick up and store the coordinates of the pixel I clicked on.

Some similar questions I found here mention use of Gtk but I’m wondering if there is a more “natural” way to accomplish this.

I’m only asking pointers and references for what’s suitable for this purpose, not for anyone to do this for me. Thank you in advance.

I think GtkReactive is what you might be looking for:
https://juliagizmos.github.io/GtkReactive.jl/stable/index.html

Issue similiar to your problem mentioned here:
https://github.com/JuliaImages/ImageView.jl/issues/240

Thank you @Ashwani_Rathee. I searched here but not at GitHub.

GLMakie can also do this.

This piece of code plots a red cross on the position you click in the image:

using GLMakie

img = rand(50,50)

fig, ax = image(img)

display(fig)

on(events(fig).mousebutton, priority=0) do event
    if event.button == Mouse.left
        x, y = mouseposition(ax.scene)
        scatter!(ax, [x], [y], marker='+', color=:red, markersize=30)
    end
end
2 Likes

Many thanks @MLackner, I’m more comfortable working with GLMakie than Gtk. I appreciate it.