如何循环播放图像序列?

时间:2019-06-05 20:16:29

标签: python image loops

我要连续循环图库(文件夹),直到按一个键。 所以我有一个包含3个图像1,2,3的文件夹。我想按顺序显示它们,然后重复。

我已经使用了一段时间,但没有设法使其正常工作。

import Image

image1 = Image.open('image1.jpg')
image.show()
image2 = Image.open('image2.jpg')
image.show()
image3 = Image.open('image3.jpg')
image.show()

2 个答案:

答案 0 :(得分:0)

查看是否可行。我敢肯定有一种更简单的方法,但这就是我能想到的。

from os import listdir
from os.path import isfile, join, abspath
import time
import subprocess

mypath = abspath(__file__)
files_in_folder = [f for f in listdir() if isfile(join(mypath, f))]

# get images
imgs = []
for f in files_in_folder:
    _, file_extension = os.path.splitext(f)
    if file_extension = ".jpg"
    imgs.append(f)

# run loop until keyboard interrupt
try:
    while True:
        for img in imgs:
            viewer = subprocess.Popen(['some_viewer', img])
            viewer.terminate()
            time.sleep(3)
            viewer.kill()
except KeyboardInterrupt:
    pass

答案 1 :(得分:0)

以下是使用glob模块的方法:

import Image
from glob import glob

path = 'C:\\Users\\User\\Desktop\\Folder'
#images = []

for ing in glob(path+'\\*.jpg'):
    image = Image.open('image1.jpg')
    #images.append(image)
    image.show()

注释的代码行用于如果您希望以后可以在代码中访问图像。