如何使用Python从文件夹中自动打印新的JPEG文件

时间:2018-01-05 21:19:05

标签: python printing raspberry-pi jpeg cups

我正在使用带有python 3.5的Raspberry Pi3

对于一个项目,我需要自动打印将在我的Raspberry Pi3上的文件夹中的每个新JPEG图像。

我做了很多研究,但还没有得到最终答案。 我所拥有的是通过USB和CUPS连接到Pi的打印机(工作正常)

我现在需要的是一个python脚本,它将检查是否有新文件夹然后如果是,则自动打印它。

我找到的很多关于FindFirstChangeAutofication的信息 我试过这个脚本: (将要观看的路径更改为需要观看的文件夹)

import os
import time

path_to_watch = ('/home/pi/jebenter/')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
    time.sleep (10)
    after = dict ([(f, None) for f in os.listdir (path_to_watch)])
    added = [f for f in after if not f in before]
    removed = [f for f in before if not f in after]
    if added: print "Added: ", ", ".join (added)
    if removed: print "Removed: ", ", ".join (removed)
    before = after`

尚未准备好但不确定如何完成它。
我需要一种方法将它发送到我的打印机 现在有人怎么帮忙?

1 个答案:

答案 0 :(得分:0)

正如我在上面的评论中所写,您可以使用lp或类似的东西打印文件。

我没有对此进行测试,但应该运行:

import os
import time
import subprocess

path_to_watch = ('/home/pi/jebenter/')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
    time.sleep (10)
    after = dict ([(f, None) for f in os.listdir (path_to_watch)])
    added = [f for f in after if not f in before]
    removed = [f for f in before if not f in after]
    if added:
        print "Added: ", ", ".join (added)
        subprocess.Popen(["lp", "-d", printer_name, "--"] + added).communicate()
    if removed: print "Removed: ", ", ".join (removed)
    before = after