0

USB Tiny Thermal Printer + Pi + Button for Image Printing
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

USB Tiny Thermal Printer + Pi + Button for Image Printing

by cbud on Sat May 21, 2022 4:55 pm

Hello! I am trying to create a thermal printer fortune teller - I've made similar projects in the past with an Arduino + thermal printer using simple text strings as fortunes. However, this time I would like to print out a random image - so I've shifted over to a Raspberry Pi, and using this USB Thermal Printer. I'm a bit of a newbie when it comes to Raspberry Pi - and especially Python - but I feel like I'm close to a solution, just not quite there yet.

I noticed most of the Adafruit Thermal Printer tutorials for the Raspberry Pi were for the TTL version, and didn't really deal with printing images, mostly just text. I followed this YouTube video on installing the Raspberry Pi OS, connecting the thermal printer via USB, installing CUPS, setting it as the default printer - and I was able to print out both the stock Raspberry Pi logo along with some custom images using the "lp -o fit-to-page /usr/share/raspberrypi-artwork/raspberry-pi-logo.png" command line prompt.

I've also mostly figured out the button side in Python. This is my first time working with Python, so I've been following this example on reading a button press, and it's been successful.

Where I am struggling is how to send a command through python for the printer to print an image. I found this example, but I encounter problems with the "printer_name=printers.keys()[0]" line. I have a suspicion this is something to do with Python 2 vs. Python 3, but I'm in way over my head there.

Any tips on sending a random image via CUPS to a thermal printer using Python? Is it possible to send something similar to the "lp -o fit-to-page /usr/share/raspberrypi-artwork/raspberry-pi-logo.png" command line prompt in python? Thank you for any help!

cbud
 
Posts: 14
Joined: Tue Jan 31, 2012 4:28 pm

Re: USB Tiny Thermal Printer + Pi + Button for Image Printin

by adafruit_support_mike on Sat May 21, 2022 11:23 pm

The general idea is to send the data to the `lpr` process. That will handle the work of passing the data on to the printer.

This page from Stack Overflow has sample code:

https://stackoverflow.com/questions/127 ... n#22550163

Code: Select all | TOGGLE FULL SIZE
import subprocess
lpr =  subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)

adafruit_support_mike
 
Posts: 64803
Joined: Thu Feb 11, 2010 2:51 pm

Re: USB Tiny Thermal Printer + Pi + Button for Image Printin

by cbud on Sun May 22, 2022 11:17 am

Thank you for the response! I'll have to give that a try.

I also discovered the os.system command, which seems to do what I was looking for, but lpr does seem to be more straightforward.

Here's the code that ended up doing what I wanted (printing a random image from the "/home/admin/Pictures/" folder to the thermal printer):

Code: Select all | TOGGLE FULL SIZE
from gpiozero import Button
from time import sleep
import os
import random

button = Button(2)
current_state = 0
files = os.listdir("/home/admin/Pictures/")
index = random.randrange(0, len(files))
random_file = files[index]

while True:
    if button.is_pressed:
        if (current_state == 0):
            print(random_file)
            os.system("lp -o fit-to-page /home/admin/Pictures/" + random_file )
            index = random.randrange(0, len(files))
            random_file = files[index]
            current_state = 1
            sleep(0.5)
    else:
        current_state = 0
        sleep(0.5)


cbud
 
Posts: 14
Joined: Tue Jan 31, 2012 4:28 pm

Re: USB Tiny Thermal Printer + Pi + Button for Image Printin

by adafruit_support_mike on Tue May 24, 2022 4:44 pm

Yep, you're on the right track.

os.system is more general than a pipe to `lpr`, but they can both get the job done.

adafruit_support_mike
 
Posts: 64803
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.


cron