brother_ql_wrapper.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import subprocess, os
  2. from brother_ql.conversion import convert
  3. from brother_ql.backends.helpers import send
  4. from brother_ql.raster import BrotherQLRaster
  5. class BrotherQlPrinter():
  6. backend = 'pyusb'
  7. def __init__(self, model='QL-810W', printer_connection='usb://0x04f9:0x209c', label_format='12', red_label=False, dpi_600=False):
  8. self.model = model
  9. self.printer = printer_connection
  10. self.label = label_format
  11. self.red_label = red_label
  12. self.dpi_600 = dpi_600
  13. def print_image(self, image, rotation='Auto', cut=True):
  14. qlr = BrotherQLRaster(self.model)
  15. qlr.exception_on_warning = True
  16. instructions = convert(qlr=qlr,
  17. images = [image],
  18. label=self.label,
  19. rotate = str(rotation),
  20. threshold=70.0,
  21. dither = False,
  22. compress = False,
  23. red = self.red_label,
  24. dpi_600=self.dpi_600,
  25. hq = True,
  26. cut = cut
  27. )
  28. send(instructions=instructions, printer_identifier=self.printer, backend_identifier=self.backend, blocking=True)
  29. def print_label(label):
  30. printer = BrotherQlPrinter(label_format=label.ql_format, red_label=label.red_label, dpi_600=label.dpi_600)
  31. printer.print_image(label.get_pillow_image(), rotation=label.ql_rotation, cut=True)
  32. def print_labels(labels):
  33. printer = BrotherQlPrinter(label_format=labels[0].ql_format, red_label=labels[0].red_label, dpi_600=labels[0].dpi_600)
  34. for label in labels[:-1]:
  35. printer.print_image(label.get_pillow_image(), rotation=label.ql_rotation, cut=False)
  36. printer.print_image(labels[-1].get_pillow_image(), rotation=labels[-1].ql_rotation, cut=True)