Toto pnp
Toto pnp

Reputation: 1

Can python read images from directory in real-time?

I have written code for taking snap-shot from a selected video and save to image using OpenCV and save the file in a chosen directory, but I don't know how Python can get the list of files in the directory in real-time. Now I have written Python code to get the list of files in the directory but as soon as the file list is updated, the program ends. It is unable to read the directory when images in it get updated or there is a new Image created inside it.

My code:

from PIL import Image
import glob
image_list = []

for filename in glob.glob('path/*.jpg'):
    im=Image.open(filename)
    image_list.append(im)
print(image_list)

Upvotes: 0

Views: 438

Answers (1)

Carlos
Carlos

Reputation: 1935

Sounds like you need to use watchdog:

https://pythonhosted.org/watchdog/quickstart.html#a-simple-example

In a nutshell, you want to write your Python script to monitor a particular directory and watch for on_created events. Once that event is found, read the file and continue monitoring the directory for additional on_created events.

Upvotes: 1

Related Questions