Python Forum
Script broke with Python 3 / improve script for printing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script broke with Python 3 / improve script for printing
#1
Lightbulb 
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()
Reply
#2
(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?
Reply
#3
did you post your question on stackoverflow? I think I have seen it there. If you did - please post a link here
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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)
Reply
#5
(Feb-22-2019, 01:57 AM)aspecteleven Wrote: I have not posted there.
Maybe I was confused between here and there :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PowerShell script file for updating packages pnachtwey 8 3,157 Feb-18-2024, 10:26 PM
Last Post: pnachtwey
  Watch files and automatically run a script in Linux Gribouillis 4 4,658 Aug-14-2023, 11:57 AM
Last Post: Gribouillis
  lcd script JarredAwesome 2 2,990 Oct-25-2020, 10:58 AM
Last Post: Larz60+
  Mad-lib script (beginner) eddiemyson 1 2,670 Jun-19-2020, 12:18 PM
Last Post: Yoriz
  Just a simple vshare.io api script Aqua22 5 4,289 May-11-2020, 04:34 PM
Last Post: buran
  A script I made to run with a cron menator01 2 2,978 Oct-19-2019, 11:20 PM
Last Post: menator01
  Please help me refactor my clunky fridge script Brompy 3 2,718 Oct-12-2019, 01:23 PM
Last Post: ichabod801
  Optimize the - Split brain detection script 0gg1 2 2,965 Oct-08-2019, 01:51 PM
Last Post: 0gg1
  script to check bash scripts Skaperen 3 2,759 Sep-19-2019, 10:26 PM
Last Post: Skaperen
  my earliest completed script Skaperen 0 1,987 Mar-08-2019, 09:50 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020