Reputation: 39
For a specific experience, I need to capture images very often (like a time lapse) and I want to send some of them to a printer from time to time. So the idea was to download images from the camera to a folder (first step). Then to ask the code to make a random choice of one of these images and to send it to a printer in a random lapse of time (means not immediatly).(Step2) And then send the image to an other folder to avoid to print it twice.(step3) Am I clear ?
Well I must say I'm not so good so a friend of mine help. Here is our code, but for the moment it doesn't work and I don't understand why. Could someone put us on the right track ? Please.
import os
import sys
from random import shuffle, randint
import time
import shutil
def checkDirectory(path):
listExt = ('.jpg', '.JPG')
listFiles = os.listdir(path)
listImages = []
for f in listFiles:
name, ext = os.path.splitext(f)
if ext in listExt:
myPath = os.path.join(path, f)
listImages.append(myPath)
return listImages
def printImage(imageFile):
command = "lpr {}".format(imageFile)
os.system(command)
def printImages(path, pathDst):
listFiles = shuffle(checkDirectory(path))
print(listFiles)
if listFiles:
for f in listFiles:
t = randint(60, 180)
time.sleep(t)
printImage(f)
shutil.move(f, pathDst)
printImages(r"/Users/Aym/Desktop/eden/", r"/Users/Aym/Desktop/eden2/")
Upvotes: 0
Views: 49
Reputation: 55207
random.shuffle
is used to change the order of a list
, not to pick random elements from it.
Instead, use random.choice
.
import random
# ...
def printImages(path, pathDst):
image_files = checkDirectory(path)
if image_files:
t = randint(60, 180)
time.sleep(t)
image_for_print = random.choice(image_files)
printImage(image_for_print)
shutil.move(image_for_print, pathDst)
Then, you'll want to repeat that so that you send an image "from time to time":
while True:
printImages(r"/Users/Aym/Desktop/eden/", r"/Users/Aym/Desktop/eden2/")
Upvotes: 1