如何更改此 python 行以让它在不说明特定图像名称的情况下读取其最新时间戳的图像?


img = cv2.imread('wayne.jpg')

这是我的代码,读取此图像for opencv python,我该如何更改它,以便系统会自动选择要读取的最新照片,而不必陈述特定的图像?

您可以使用OS模块在文件夹中列出所有图像文件并选择最新的文件。

folder = '/example/folder/path'
files = [os.path.join(folder, f) for f in os.listdir(folder)]
filter(lambda x:x.endswith('.jpg'), files) # check if file is .jpg extension
files.sort(key=lambda x: os.path.getmtime(x))
latest_file = files[-1]

然后您可以

img = cv2.imread(latest_file)

相关内容

最新更新