Atlas - receipt.py
Home / lab / sparkylab Lines: 4 | Size: 2789 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1# SPDX-License-Identifier: GPL-3.0 2# ESC/POS Image experiment 3# 4# receipt.py 5# A simple script to print text and images on an ESC/POS compatible printer. 6# 7# Authored: sparkydadoggo <[email protected]> 8# Co-Authored: 0x4248 <[email protected]> 9# Reviewed by: 0x4248 <[email protected]> 10# 11# TESTED HARDWARE: 12# - TOSHIBA ???? (I dont know what the actual model is, there is so manty numbers on the printer but 13# all look like serial numbers, has ESC/POS support, "images" as we saw and serial over standard 2.0 USB, RS232) 14# 15# COPYRIGHT NOTICE 16# Copyright (C) 2025-2026 0x4248 and contributors 17# Redistribution and use in source and binary forms, with or without 18# modification, are permitted provided that the license is not changed. 19# 20# This software is free and open source. Licensed under the GNU general 21# public license version 3.0 as published by the Free Software Foundation. 22# 23# NOTE: 24# made with Sparky's own 2 paws 25 26from PIL import Image 27from escpos import * 28p = printer.Usb(0x08a6, 0x003d, profile ="default") 29 30while True: 31 usertext = input("Enter text to print, enter 'cut' to cut the paper, enter 'exit' to quit: ") 32 if usertext.lower() == 'exit': 33 break 34 elif usertext.lower() == 'cut': 35 for i in range(3): 36 p.text("\n") 37 p.cut() 38 elif usertext.lower() == 'image': 39 # XXX: If the image size is wrong or too large, expect an entire roll of paper to be lost. 40 # You will get an output of "random characters" and the printer will just keep printing. We 41 # assume that its just the printer printing the raw data of the image, but we dont know for 42 # sure. 43 p.profile.profile_data['media']['width']['pixels'] = 576 # or 384, test at your own risk 44 img = Image.open(input("Enter the path to the image you want to print: ")) 45 # HACK: This is the weirdest part of the code, but it works. The printer can only print 46 # images that are 576 pixels wide (or 384 for some models), so we need to resize the image 47 # to fit the printer's width while maintaining the aspect ratio. 48 # 49 # We really should figure out if there is a better way to do this e.g detecting the 50 # printer's width and resizing the image accordingly, but for now this is a simple solution 51 # that "should" work with most images. 52 printer_width = 576 53 aspect_ratio = img.height / img.width 54 new_height = int(printer_width * aspect_ratio) 55 img = img.resize((printer_width, new_height)) 56 img = img.convert("1") 57 p.image(img, impl="bitImageColumn") 58 for i in range(3): 59 p.text("\n") 60 p.cut() 61 else: 62 p.text(usertext + "\n")[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.