python - 如何找到最新的照片并打开它 - 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 上找到了这些代码
解决方案
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.
推荐阅读
- vba - 根据其他列更改单元格值
- opencv - 如何使用 OpenCV 和 C++ 裁剪网络摄像头馈送帧
- html - 如何通过selenium webdriver和java点击页面上带有锚标签的链接
- java - 单元测试中是否使用了 Spring Framework?
- akka - akka.persistence.RecoveryTimedOut:恢复超时,30000毫秒内没有拿到快照
- angular - angular 6 通用应用程序生产构建错误
- selenium - 如何获取跨度内的文本
- python - 如何在不同的应用程序中从 LabelEncoder() 导出和使用 classes_?
- vba - 使用 Visual Basic 在 Outlook 中的附件和电子邮件主题
- linux - 将两个特定的 Linux 命令合二为一