Raspberry Pi Photobooth Printing

时间:2016-08-24 07:39:33

标签: python printing raspberry-pi

我已经构建了this照片,我正在努力弄清楚我需要添加到脚本中的代码才能打印出每张照片的副本。我已经使用杯子将我的打印机映射到树莓派。

Here是脚本的github。

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,您需要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)