2014-02-18 34 views
0
以随机化

我已经成功地写小代码,但我的技能是非常差的(虽然我尽我所能)需要一些精度在Python

我想打印从相机dowloaded图像。但是,虽然相机会每30秒拍摄一张图像,但我想随机打印一段时间(3到10分钟之间)图像

有人可以帮我吗?非常感谢

这是我迄今为止所做的代码。

monRep = "***/folder1" 
import os, mimetypes, random, glob 
while True: 
    fpaths = [] 
    for fname in os.listdir(monRep): 
     fpath = os.path.join(monRep, fname) 
     if os.path.isfile(fpath): 
      mt = mimetypes.guess_type(fpath)[0] 
      ext = os.path.splitext(fpath)[1] 
      if mt: mt = mt.split('/')[0].lower() 
      else: mt = False 
      #if ext.lower() in ('.bmp','.pict', '.JPG', '.jpg', '.pdf'): mt = 'image' 
      if mt in ('image',): fpaths.append(fpath) 

    for fpath in fpaths: 
     newpath = fpath.replace('***/Folder1/','***/Folder2/') 
     os.rename(fpath,newpath) 
     command = "lpr "+newpath 
     print (command) 
     os.system(command) 

    import time 
    time.sleep (1) 

    directory='***/Folder2/' 
    os.chdir(directory) 
    files=glob.glob('*.JPG') 
    for filename in files: 
     os.unlink(filename) 
+0

?打印文件名,在图像处理器中显示图像或将其发送到打印机? – RickyA

+0

对不起:把它发送到打印机谢谢 – NocNockie

+1

好吧,那么你的脚本中现在不工作? – RickyA

回答

0

运行这一个又一个脚本,因为它应该独立于你的导入代码:你说的打印的意思

import glob 
import random 
import time 

while True: 
    directory='***/Folder1/' 
    files = glob.glob('*.JPG') #get all jpg files in your dir 
    selected_file = random.choice(files) #selects a random entry in 'files' 
    #print selected file 
    sleeptime = random.randint(3*60, 11*60) #selects random number between 3 and 11 min (in sec) 
    time.sleep(sleeptime) #and sleep for that time 
+0

Rickie 我想非常感谢你......我会尽快尝试这个 一个问题的想法是分离运行这两个代码,但同时,我是否正确? – NocNockie

+0

是的,你在一个脚本中运行导入器,而在另一个脚本中运行导入器。否则,此代码中的睡眠会导致您的导入作业暂停。 – RickyA

+0

我在想,以前的脚本应该直接发送每一个图像到打印机......我应该增加睡眠时间,让第二个脚本工作? – NocNockie