Feb-05-2019, 03:27 AM
(This post was last modified: Feb-05-2019, 03:27 AM by aspecteleven.)
Hello, everyone! This is my first post on here, and the first Python script I've written, having just started learning Python three weeks ago.
Hardware: Raspberry Pi 3, DYMO 450 Turbo Label Printer (via CUPS), wireless 2D Barcode Scanner
OS: Raspbian Stretch with desktop and recommended software
Function of the script:
To take text input given by a user with a barcode scanner, which scans pre-made QR codes. The script then breaks down the input and puts it into a specific layout using PyCairo. It also generates a QR Code using PyQRCode, which contains the same data that was scanned in by the user. The scanned data is in the format {123|456|789}. The Cairo layout and the QR Code are then combined into one image and printed to a DYMO 450 Turbo label printer.
I've had the script working, but now have some problems.
1) I started out with Python 2 and didn't realize it. I upgraded to Python 3. The script now doesn't work correctly. Everything works up until trying to combine the images into one file (.png). It just creates a black rectangle. No idea why. Everything else works.
2) In context to the final output, I would prefer to keep the format as a vector, but was not able to find a way to combine two EPS files like I was doing with the .png. Any suggestions?
3) The biggest problem I was having prior to updating to Python 3 was print speed. The first print of the "test.png" image was about 2-3 seconds, and then 10 seconds wait time each consecutive print. At present this might not have much to do with Python, but I wasn't sure if there was a better way to create the file(s) so the final result would be able to print faster (1 second or less wait time between each label printing. This is something I was able to achieve with Windows and JavaScript, but as the Dymo SDK and framework are not available on Linux it isn't so simple).
Here is my code:
Hardware: Raspberry Pi 3, DYMO 450 Turbo Label Printer (via CUPS), wireless 2D Barcode Scanner
OS: Raspbian Stretch with desktop and recommended software
Function of the script:
To take text input given by a user with a barcode scanner, which scans pre-made QR codes. The script then breaks down the input and puts it into a specific layout using PyCairo. It also generates a QR Code using PyQRCode, which contains the same data that was scanned in by the user. The scanned data is in the format {123|456|789}. The Cairo layout and the QR Code are then combined into one image and printed to a DYMO 450 Turbo label printer.
I've had the script working, but now have some problems.
1) I started out with Python 2 and didn't realize it. I upgraded to Python 3. The script now doesn't work correctly. Everything works up until trying to combine the images into one file (.png). It just creates a black rectangle. No idea why. Everything else works.
2) In context to the final output, I would prefer to keep the format as a vector, but was not able to find a way to combine two EPS files like I was doing with the .png. Any suggestions?
3) The biggest problem I was having prior to updating to Python 3 was print speed. The first print of the "test.png" image was about 2-3 seconds, and then 10 seconds wait time each consecutive print. At present this might not have much to do with Python, but I wasn't sure if there was a better way to create the file(s) so the final result would be able to print faster (1 second or less wait time between each label printing. This is something I was able to achieve with Windows and JavaScript, but as the Dymo SDK and framework are not available on Linux it isn't so simple).
Here is my code:
import cairo import datetime import pyqrcode import subprocess import sys from PIL import Image def qrTags(): scannedInput = input('Enter Data: ') scannedInputQR = scannedInput inputLength = len(scannedInput) findLeft = scannedInput.find('{') findVBar = scannedInput.find('|') findRight = scannedInput.find('}') scannedInput = scannedInput.split('|') iText = scannedInput[0].split('{') lText = scannedInput[1] aText = scannedInput[2].split('}') now = datetime.datetime.now() dateTimeStamp = now.strftime("%m/%d/%y @ %I:%M:%S %p") SID = "SID: 00" if findLeft == 0 and findVBar >= 0 and findRight == (inputLength-1): with cairo.PSSurface("textBase.eps",250,123) as surface: context = cairo.Context(surface) context.rotate(3.14) context.translate(-250,-123) mLX = 40 iX,lX,allX,dateX,copyX,sidX = mLX,mLX,mLX,mLX,mLX,150 iY,lY,allY,dateY,copyY,sidY = 25,48,72,95,110,25 context.rectangle(30,0,250,123) context.set_source_rgba(0,0,0,1) context.set_line_width(2) context.stroke() context.set_source_rgba(0,0,0,1) context.set_line_width(0.04) context.select_font_face ("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_BOLD) context.set_font_size (17) context.move_to(iX,iY) context.show_text(iText[1]) context.set_source_rgba(0,0,0,1) context.select_font_face ("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL) context.set_font_size (14) context.move_to(lX,lY) context.show_text(lText) context.set_source_rgba(0,0,0,1) context.set_line_width(1) context.move_to(iX,lY + 7) context.line_to(190,lY + 7) context.stroke() context.set_source_rgba(0,0,0,1) context.select_font_face ("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL) context.set_font_size (14) context.move_to(allX,allY) context.show_text(aText[0]) context.set_source_rgba(0,0,0,1) context.set_line_width(1) context.move_to(iX,allY + 7) context.line_to(190,allY + 7) context.stroke() context.set_source_rgba(0,0,0,1) context.select_font_face ("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL) context.set_font_size (12) context.move_to(dateX,dateY) context.show_text(dateTimeStamp) context.set_source_rgba(0,0,0,1) context.select_font_face ("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL) context.set_font_size (12) context.move_to(sidX,sidY) context.show_text(SID) context.set_source_rgba(0,0,0,1) context.select_font_face ("Sans",cairo.FONT_SLANT_NORMAL,cairo.FONT_WEIGHT_NORMAL) context.set_font_size (7) context.move_to(copyX,copyY) context.show_text("Copyright 2019") text = pyqrcode.create(scannedInputQR) text.eps('qrCode.eps', scale=3) images = map(Image.open,['qrCode.eps','textBase.eps']) widths,heights = zip(*(i.size for i in images)) total_width = sum(widths) max_height = max(heights) new_im = Image.new('RGB', (total_width,max_height)) x_offset = 0 for im in images: new_im.paste(im, (x_offset,0)) x_offset += im.size[0] new_im.save('test.png') print("made file") subprocess.run(["lp","test.png"]) else: print("Fail") flag = True while flag: qrTags()