Last active
November 22, 2017 09:23
-
-
Save serser/0f896900559f2873c1e946b34b8a07c3 to your computer and use it in GitHub Desktop.
using matplotlib to continously show a series of images without closing current window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# press any key like ESC to exit | |
import matplotlib.pyplot as plt | |
import matplotlib.patches as patches | |
import scipy.misc | |
import glob | |
import os | |
import sys | |
IMAGE_ROOT = "/path/to/images" | |
looping = True | |
# use any key like ESC etcetera to escape the loop | |
def press(event): | |
global looping | |
print 'pressed', event.key | |
looping = False | |
sys.exit('Exiting the program') | |
fig, ax = plt.subplots(1) | |
# register callback when key presses | |
fig.canvas.mpl_connect('key_press_event', press) | |
plt.ion() | |
for fname in glob.glob(IMAGE_ROOT+'/*'): | |
img = scipy.misc.imread(os.path.join(IMAGE_ROOT, fname)) | |
plt.imshow(img) | |
if not looping: | |
break | |
# setting non-blocking mode | |
plt.show(block=False) | |
# pause for 0.5 sec per image | |
plt.pause(0.5) | |
ax.cla() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment