【发布时间】:2016-08-24 07:39:32
【问题描述】:
我已经建立了this photobooth,我正在努力弄清楚我需要向脚本中添加什么代码才能打印每张照片的副本。我已经使用杯子将我的打印机映射到树莓派上。
Here 是带有脚本的 github。
谢谢!
【问题讨论】:
标签: python printing raspberry-pi
我已经建立了this photobooth,我正在努力弄清楚我需要向脚本中添加什么代码才能打印每张照片的副本。我已经使用杯子将我的打印机映射到树莓派上。
Here 是带有脚本的 github。
谢谢!
【问题讨论】:
标签: python printing raspberry-pi
首先,您需要pycups。那么这段代码应该可以工作,但我无法对其进行测试:
# Set up CUPS
conn = cups.Connection()
printers = conn.getPrinters()
printer_name = printers.keys()[0]
cups.setUser('pi')
# Save the picture to a temporary file for printing
from tempfile import mktemp
output = mktemp(prefix='jpg')
im.save(output, format='jpeg')
# Send the picture to the printer
print_id = conn.printFile(printer_name, output, "Photo Booth", {})
# Wait until the job finishes
from time import sleep
while conn.getJobs().get(print_id, None):
sleep(1)
图片是im,创建于line 168。只需将代码粘贴到此行下方即可。
更多细节可以在boothcam.py#L99中找到snap方法。
这是我成功测试的脚本:
#!/usr/bin/env python
# coding: utf-8
import cups
import Image
from tempfile import mktemp
from time import sleep
# Set up CUPS
conn = cups.Connection()
printers = conn.getPrinters()
printer_name = printers.keys()[0]
cups.setUser('tiger-222')
# Image (code taken from boothcam.py)
im = Image.new('RGBA', (683, 384))
im.paste(Image.open('test.jpg').resize((683, 384)), ( 0, 0, 683, 384))
# Save data to a temporary file
output = mktemp(prefix='jpg')
im.save(output, format='jpeg')
# Send the picture to the printer
print_id = conn.printFile(printer_name, output, "Photo Booth", {})
# Wait until the job finishes
while conn.getJobs().get(print_id, None):
sleep(1)
unlink(output)
【讨论】: