Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Assuming you have a list of image files, you can use the following Python code to randomly select and print one of them using a thermal printer:

import random
from PIL import Image
from Adafruit_Thermal import *

# Initialize the printer
printer = Adafruit_Thermal('/dev/ttyUSB0', 19200, timeout=5)

# List of image files
image_files = ['image1.bmp', 'image2.bmp', 'image3.bmp', 'image4.bmp']

# Select a random image
random_image = random.choice(image_files)

# Load and print the image
image = Image.open(random_image)
printer.printImage(image, thermalPrinter.NLQ)

Note that this code assumes you are using the Adafruit_Thermal library to interface with the thermal printer, and that the image files are in BMP format (which is the format supported by the printImage method). If your images are in a different format, you'll need to convert them to BMP before printing them.