0

The code generates a QR code and prints it, but It is not working on the Debian Os due to not supporting the imported libraries (win32print, Win32ui). Can anyone tell me how to run it on the Debian without changing the whole code.


from random import randint
import win32print
import win32ui

from PIL import Image, ImageWin
from PIL._imaging import font
from PIL import ImageFont
from PIL import ImageDraw



    HORZRES = 8
    VERTRES = 10

   LOGPIXELSX = 88
   LOGPIXELSY = 90

   PHYSICALWIDTH = 110
   PHYSICALHEIGHT = 111


   PHYSICALOFFSETX = 112
   PHYSICALOFFSETY = 113


__author__ = 'masoodhussain'

import qrcode
import subprocess
import os

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)

qr.add_data('Masooddkjfdlfs,kokdfds sddshfhkjshfljsdhkjfdrtyyhtfhfghgh3')
qr.make(fit=True)

"subprocess.call(['lp', 'foo.png'])"

printer_name = win32print.GetDefaultPrinter()

img = qr.make_image()

img.show()

random_number= randint(0,10000)

img.save('label_'+str(random_number)+'.png')

file_name = 'label_'+str(random_number)+'.png'

print(file_name)


hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)


bmp = Image.open (file_name)


if bmp.size[0] > bmp.size[1]:
  bmp = bmp.rotate (90)

ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)


hDC.StartDoc (file_name)
hDC.StartPage ()

dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))

hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()

when I run the code by removing the unsupported libraries it gives an error on this part: error importing

import qrcode

I am trying to import whole folder for using there other files. In Windows it was working perfectly. Any help would be appreciated.Thanks

1
  • First you need to make sure you have qrcode installed. You can check this by running the following command from the temrinal: pip freeze | grep qrcode Commented May 1, 2016 at 16:19

2 Answers 2

1

This code is equivalent to the code posted in Question.

from random import randint
import cups

from PIL import Image, ImageWin
from PIL._imaging import font
from PIL import ImageFont
from PIL import ImageDraw


__author__ = 'masoodhussain'

import qrcode

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=5,
    border=2,
)

qr.add_data('localhost:5070productinfo')
qr.make(fit=True)

conn= cups.Connection()
printer_name = conn.getPrinters()

printer_name = printer_name.keys()[0]

printqueuelength = len(conn.getJobs())

img = qr.make_image()

img.show()

random_number= randint(0,10000)

img.save('label_'+str(random_number)+'.png')

file_name = 'label_'+str(random_number)+'.png'

print(file_name)

conn.printFile(printer_name,file_name,"Hello", options ={'media':'25x25mm'}) 

Important part is the installation of required libraries and changing your media to the required size.

Sign up to request clarification or add additional context in comments.

Comments

0

Even if you install qrcode, your code will still fail because of the Windows specific library. You need to check on which system you are working and preferably put the whole print function in a separate function.
Here are some useful links: https://stackoverflow.com/a/1857/2776376 and https://pypi.python.org/pypi/pycups

import platform

if platform.system() = 'Linux':
    import libcups
elif platform.system() = 'Windows':
    import win32print
    import win32ui
else:
    print('Unsupported OS. Exiting....')
    sys.exit(1)

def my_printer_function():
    if platform.system() = 'Linux':
        #now call the Linux printer
    elif platform.system() = 'Windows':
        #use your old Windows code

9 Comments

I have a problem. I have converted the code using pycups and it is printing the qr code but it is printing only a portion of the image. How can I adjust the image in pycups, I have tried conn.printFile(printer_name,file_name,"Hello ", {'fit-to-page':'True'}) but the result is same
The last portion of the above code was adjusting the image but now I have removed all this portion so its not adjusting the image
is the printed image bigger than the page or does it place the image on the wrong part of the page?
the printed image is bigger than the page. only 1/4 Qr code is printed on the full Qr sticker
Does this also happen when you call cups from the command line? You could also try using scaling=100.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.