2

I've built this photobooth, and I am struggling to figure out what code i would need to add to the script in order to get this to print a copy of each photo. I have already mapped my printer to the raspberry pi using cups.

Here is the github with the script.

Thanks!

odunde
  • 29
  • 1
  • 3

1 Answers1

4

First, you will need pycups. Then this code should work but I cannot test it:

# 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)

The picture is im, which is created at line 168. Just paste the code below this line.

For more details, you can find the snap method in boothcam.py#L99.

This is a script I succesfully tested:

#!/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)
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • This is amazing. Thanks so much! I built this so that it takes four photos and merges them into one image-with each photo in a quadrant of the merged image. I'm trying to add code to the python script so that each of the four photos will have a border around them. Nothing I've tried comes close to working. Any suggestions? – odunde Aug 25 '16 at 18:11
  • You are welcome :) Can you set the answer as answered if you think it is good? For your second question, check this link: [Adding borders to an image using python](https://stackoverflow.com/questions/11142851/adding-borders-to-an-image-using-python). – Tiger-222 Aug 26 '16 at 07:24
  • This was very helpful for me. I was looking to do this with arbitrary images, but just getting the basics was very helpful. – Rex Schrader Sep 16 '22 at 18:11