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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With