首页 > 解决方案 > 如何找到最新的照片并打开它 - Photo Booth

问题描述

我找到了找到保存在文件夹中的最后一个文件的代码,但我需要打开这个文件 x 时间,然后关闭它。这可以做到吗?

这是我用来查找最新 .jpg 的代码

import glob
import os

list_of_files = glob.glob('/home/pi/webcam/*.jpg')
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file

我尝试了以下方法,代码运行但没有任何反应:

from PIL import Image
import glob
import os

list_of_files = glob.glob('/path/to/folder/*.jpg')
latest_file = max(list_of_files, key=os.path.getctime)

img = Image.open(latest_file)
img.show()

我正在尝试将其构建到boot.py

这是我到目前为止的尝试 (有以下建议)

我在 Stack Overflow 上找到了这些代码

标签: pythonfileraspberry-piphotophotobooth

解决方案


As it stands, your program is opening a window, starting to exit and closing the window. This probably happens faster than your operating system's window opening animation.

Try:

from PIL import Image
import glob
import os
from time import sleep

list_of_files = glob.glob('/path/to/folder/*.jpg')
latest_file = max(list_of_files, key=os.path.getctime)

img = Image.open(latest_file)
img.show()
sleep(10)

If this solves your problem, then great! If you want to make this a permanent thing, start a non-daemon thread with the img.show() call that only exits when the created window is closed. (You can probably figure out how to do this... maybe. I can't!)

The reason that os.startfile(playlist) isn't working is that os.startfile is a Windows-only feature. You're using Python 2, and the only sane reason I can think of for that is to control Raspberry Pi GPIO pins; it won't be available on that platform.


推荐阅读