How to find the latest photograph and open it - Photo Booth

112 Views Asked by At

I have found the code to find the last file saved in a folder, but I need to open this file for x amount of time, then close it. Can this be done?

Here is the code I am using to find the latest .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

I have tried the following, the code runs but nothing happens:

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()

I am trying to build this into booth.py

Here is my attempt so far (with below suggestions)

I found these codes on Stack Overflow

2

There are 2 best solutions below

0
On

Installing ImageMagick solved the issue of opening the image:

sudo apt-get install  -y imagemagick
9
On

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.