time.sleep使图片无法循环显示

时间:2019-11-18 17:10:45

标签: python python-3.x matplotlib time

我一直在尝试遍历文件夹中的文件,并使用以下代码将它们分别显示两秒钟:

import time
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.image import imread
import glob
import cv2

im = []
for filepath in glob.iglob(r'C:\Users\dan20\Pictures\wedding\beeri and adn. photo/*.jpg'):
    a = imread(filepath)
    b = cv2.resize(a, dsize = (456,304))
    im += [b]

fig,ax = plt.subplots()
for i in im:
    time.sleep(2)    
    ax.axis('off')
    ax.imshow(i)
    plt.show()

由于某种原因,只要使用time.sleep(),我就看不到图像。 如何使每张图片显示N次?

1 个答案:

答案 0 :(得分:1)

如何使用plt.pause

import numpy as np
from matplotlib import pyplot as plt

im = [np.random.random((9,9)) for i in range(1,6)]

fig,ax = plt.subplots()
for i in im: 
    ax.cla()
    ax.imshow(i)
    ax.axis('off')
    plt.pause(2)

给出:

output_imshow