我想继续循环图像库(文件夹(,直到我按下一个键。所以我有一个包含 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()
看看这是否有效。我相信有一种更简单的方法,但这是我能想到的。
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
以下是如何使用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()
注释的代码行适用于您希望稍后能够在代码中访问图像的情况。