Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook: Register mouse position clicks (moves) on a displayed image (jpg/png)

Using Python 3.6+, Jupyter notebooks and matplotlib, I want to find out how to get the x,y position of an image by moving the mouse over it and/or clicking the position

I'm using any image, for example a png sized 966 x 525 pixel.

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

fig = plt.figure(figsize=(20,30))

img=mpimg.imread('ausgabe.png')
imgplot = plt.imshow(img)
plt.show();

Many suggested solutions on stackoverflow involve connecting an event to matplotlib, like

fig.canvas.mpl_connect('button_press_event', onclick)

(see Store mouse click event coordinates with matplotlib)

But in Jupyter that just doesn't react. Instead clicking on the image sometimes enlarges it.

What is a good way to display a png in Jupyter so that I could collect (=print) the click positions - or rerun the cell with the collected information?

like image 999
576i Avatar asked Oct 17 '22 13:10

576i


1 Answers

you can use the native Tk backend and you can retrieve the mouse click position this way. for example

%matplotlib tk
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

fig = plt.figure(figsize=(20,30))

img=mpimg.imread('ausgabe.png')

def onclick(event):
    ix, iy = event.xdata, event.ydata
    print(ix, iy)

cid = fig.canvas.mpl_connect('button_press_event', onclick)

imgplot = plt.imshow(img)
plt.show()

A separate tk window will pop-up and when you click, the x,y position will print in the notebook. if that doesn't work, you might need to use %matplotlib qt5 or even %matplotlib qt4

like image 164
mark jay Avatar answered Oct 21 '22 04:10

mark jay