Python Forum

Full Version: Script broke with Python 3 / improve script for printing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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()
(Feb-05-2019, 03:27 AM)aspecteleven Wrote: [ -> ]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).

I was able to get the print speed fixed. I had to use the DYMO driver bundled with Raspbian and Ubuntu and then change the quirks file so it was only Uni-directional printing only.


Any suggestions on questions 1 or 2?
did you post your question on stackoverflow? I think I have seen it there. If you did - please post a link here
(Feb-21-2019, 05:23 PM)buran Wrote: [ -> ]did you post your question on stackoverflow?

I have not posted there. The only other place I posted anything was the CUPS github See here And that is how I came to the solution of print speed (problem #3)
(Feb-22-2019, 01:57 AM)aspecteleven Wrote: [ -> ]I have not posted there.
Maybe I was confused between here and there :-)