displaying multiple images in series

Hi matplotlib users,

I'm trying to write a script to loop through a bunch of tiff files,
display each image, and choose to accept or reject each image.
Something like:

for f in files:
   im = imread(f)
   imshow(im)
   # Accept keyboard input to accept or reject image
   # Close the image

The problem is that I can't figure out how to show multiple images in
series. I can't use matplotlib.pyplot.show() because that can only be
used once at the very end of a script, and I don't want to show all
the images at once. matplotlib.pyplot.draw() seemed like a promising
candidate, but it only seems to work if I've already used show() once
in the script. It seems like there should be a simple way to do this,
but I can't quite seem to find it.

Thanks,
Daniel

Hi Daniel,

in the attached script I propose two solutions for your problem (just
uncomment first region and comment second to test the first proposal). The first
uses plt.waitforbuttonpress and the second key-press-events.

Kind regards,
Matthias

multiple_images.py (1.5 KB)

···

On Monday, June 14, 2010 10:32:48 pm Daniel Jones wrote:

Hi matplotlib users,

I'm trying to write a script to loop through a bunch of tiff files,
display each image, and choose to accept or reject each image.
Something like:

for f in files:
   im = imread(f)
   imshow(im)
   # Accept keyboard input to accept or reject image
   # Close the image

The problem is that I can't figure out how to show multiple images in
series. I can't use matplotlib.pyplot.show() because that can only be
used once at the very end of a script, and I don't want to show all
the images at once. matplotlib.pyplot.draw() seemed like a promising
candidate, but it only seems to work if I've already used show() once
in the script. It seems like there should be a simple way to do this,
but I can't quite seem to find it.

Thanks,
Daniel

Hi Matthias,
Thanks for the script! Now I would like to try to understand why it
works :slight_smile: . In both of the scripts you sent me, plt.draw() is called
inside the loop to display each image file, and plt.show() is called
outside the loop at the end of the script. I guess what I find
confusing is, how is it that the images are displayed on screen,
inside the loop, *before* plt.show() is called? I would have expected
only the very last image inside the loop to be displayed, using this
program structure. So apparently there is something here I do not
understand...

-Daniel

···

On Tue, Jun 15, 2010 at 3:26 AM, Matthias Michler <MatthiasMichler@...361...> wrote:

On Monday, June 14, 2010 10:32:48 pm Daniel Jones wrote:

Hi matplotlib users,

I'm trying to write a script to loop through a bunch of tiff files,
display each image, and choose to accept or reject each image.
Something like:

for f in files:
im = imread(f)
imshow(im)
# Accept keyboard input to accept or reject image
# Close the image

The problem is that I can't figure out how to show multiple images in
series. I can't use matplotlib.pyplot.show() because that can only be
used once at the very end of a script, and I don't want to show all
the images at once. matplotlib.pyplot.draw() seemed like a promising
candidate, but it only seems to work if I've already used show() once
in the script. It seems like there should be a simple way to do this,
but I can't quite seem to find it.

Thanks,
Daniel

Hi Daniel,

in the attached script I propose two solutions for your problem (just
uncomment first region and comment second to test the first proposal). The first
uses plt.waitforbuttonpress and the second key-press-events.

Kind regards,
Matthias

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit. See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Daniel,

show should be called only once in a script (see also:
http://matplotlib.sourceforge.net/faq/howto_faq.html?highlight=show#use-show)
and it starts the mainloop, in which the user may interact with the plot using
for instance button or key press events like in my second example. Whenever a
key-press-event is recognized the function "event_fct" is called with the
corresponding key-press-event as argument. This key-press-event holds
information like which key was used etc.. All the possible interaction you can
have with the plot in this case happens after the plt.show in the "mainloop",
where matplotlib waits for events to happen and calls the corresponding
functions, which were "connect"ed to the event. You will find more information
about the event handling in the documentation of matplotlib (e.g.
http://matplotlib.sourceforge.net/users/event_handling.html#event-handling-
tutorial or the examples at
http://matplotlib.sourceforge.net/examples/event_handling/index.html).

In my first example the trick is done by the function "waitforbuttonpress" and
all the interaction happens before the show-command. Because I'm not so
familiar with this function, I suggest you again to use the documentation and
the examples to learn more about this functionality.

Kind regards,
Matthias

···

On Monday, June 21, 2010 05:47:21 am you wrote:

Hi Matthias,
Thanks for the script! Now I would like to try to understand why it
works :slight_smile: . In both of the scripts you sent me, plt.draw() is called
inside the loop to display each image file, and plt.show() is called
outside the loop at the end of the script. I guess what I find
confusing is, how is it that the images are displayed on screen,
inside the loop, *before* plt.show() is called? I would have expected
only the very last image inside the loop to be displayed, using this
program structure. So apparently there is something here I do not
understand...