def print_pixel_coords(event):
    fig = event.canvas.figure
ax = fig.axes[0]
for i in range(28):
    x, y = ax.transData.transform_point([i, i])
print('%i, %i' % (x, fig.bbox.height - y))
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.imshow(np.random.random((27, 27)), interpolation='nearest')
fig.canvas.mpl_connect('draw_event', print_pixel_coords)
fig.savefig('foo.png', dpi=fig.dpi)
How to map coordinates in AxesImage to coordinates in saved image file?