0

I have a bunch of satellite images that I am inspecting for errors and it is pretty tedious. I would like a way to do this efficiently in Python where I can look at an image, close the window when I'm done, look at the next image, etc. without requiring me to alter the code each time (e.g. plt.imshow(image1), plt.imshow(image2), etc.).

I tried using input() which pauses a function until the user presses ENTER but unfortunately it prevents plt.imshow() from displaying the image until the loop is completely finished, and then it only shows the last image in the loop.

npoints = 100 * 100
noise = randgen.randn(npoints).reshape([100, 100])
image_1 = np.mgrid[0:100:100j, 0:100:100j][0]
image_2 = np.mgrid[0:100:100j, 0:100:100j][1]
image_3 = image_1 * image_2
images = [image_1, image_2, image_3]

for image in images:
    plt.imshow(image)
    input()

Any ideas for how I might do this, or another way to interactively plot in Python?

Thank you

la_leche
  • 423
  • 1
  • 7
  • 14
  • 1
    You could add a figure using `plt.figure()` and `plt.show()` in the end of the iteration. `for image in images: plt.figure(); plt.imshow(); plt.show()`. Furthermore you could also connect the canvas to an event that changes the image being displayed. See this: https://matplotlib.org/3.1.1/users/event_handling.html – p479h Jun 24 '20 at 07:05
  • The `plt.show()` is the important part in @p479h comment. Matplotlib plots images when control of the command line is returned to the user -- unless forced by using `plt.show` or `fig.canvas.draw()`, etc – Paul Brodersen Jun 24 '20 at 10:17
  • hmm @p479h thank you for your comment, that doesn't seem to work though. It plots all of the images at once. Unless I am doing it incorrectly, in which case maybe you could comment the full solution? I will look at matplotlib events though – la_leche Jun 24 '20 at 19:28

1 Answers1

0

I looked deeper at event handling in matplotlib and found a suitable solution using plt.waitforbuttonpress()

npoints = 100 * 100
noise = randgen.randn(npoints).reshape([100, 100])
image_1 = np.mgrid[0:100:100j, 0:100:100j][0]
image_2 = np.mgrid[0:100:100j, 0:100:100j][1]
image_3 = image_1 * image_2
images = [image_1, image_2, image_3]

for image in images:
    plt.imshow(image)
    plt.waitforbuttonpress()
    plt.close()

la_leche
  • 423
  • 1
  • 7
  • 14