2

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

CJ0206
  • 95
  • 2
  • 10
  • Do you need the last `modified` or `created`? Can you post the value of `latest_file`? . I tried your code and it worked as expected on windows `py3.6`. – Pedro Lobito Dec 10 '18 at 19:33
  • @Pedro Lobito The last created file, it is created by `os.system('fswebcam -r 640x480 --no-banner --overlay christmas.png /home/pi/webcam/%d-%m-%Y_%H:%M:%S.jpg -S 2')` – CJ0206 Dec 10 '18 at 19:46
  • Can you post the value of `latest_file`? – Pedro Lobito Dec 10 '18 at 19:48

2 Answers2

0

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.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
  • Thank you for the info about `os.startfile` I'm relatively new to this! The program now seems to just wait rather than opening anything visible. – CJ0206 Dec 10 '18 at 19:41
  • @CJ0206 Have you tried pressing Alt+Tab during the wait? Also, it would help if we could see the integrated version; perhaps make a new branch on your GitHub project or maybe add the full code to your question. – wizzwizz4 Dec 10 '18 at 19:43
  • I have added a new branch with the suggested edit in my original post – CJ0206 Dec 10 '18 at 19:52
  • @CJ0206 FYI, that's not a branch. – wizzwizz4 Dec 10 '18 at 20:06
  • @CJ0206 Are you running this in a console? You need to run it in an X session for `img.show()` to work. – wizzwizz4 Dec 10 '18 at 20:09
  • In a terminal window on the desktop. Updated to test branch too. – CJ0206 Dec 10 '18 at 20:15
  • @CJ0206 Well, I don't know why it's not opening the image. Your code seems OK. – wizzwizz4 Dec 10 '18 at 21:13
  • I installed `ImageMagick` and it now works, I just need to find a way to close it after a few seconds. – CJ0206 Dec 10 '18 at 21:40
  • @CJ0206 Please post an answer saying that you installed `ImageMagick` and it fixed it! ([By the way, due to how `img.show()` works it's impossible to easily close the window from Python.](https://stackoverflow.com/q/12570859/5223757#comment16939343_12571366)) – wizzwizz4 Dec 10 '18 at 21:42
0

Installing ImageMagick solved the issue of opening the image:

sudo apt-get install  -y imagemagick
CJ0206
  • 95
  • 2
  • 10