Added printing images
This commit is contained in:
@@ -3,6 +3,7 @@ from flask import flash
|
||||
from escpos.printer import Usb, USBNotFoundError
|
||||
from time import sleep, gmtime, strftime
|
||||
import os.path
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class Printer(object):
|
||||
@@ -130,23 +131,48 @@ class Printer(object):
|
||||
self.printer.close
|
||||
except Exception as e:
|
||||
flash("Unable to print because : " + e)
|
||||
|
||||
|
||||
flash("Message printed : " + clean_msg ,category='info')
|
||||
return True
|
||||
|
||||
def print_img(self, path):
|
||||
def print_img(self, path, sign):
|
||||
clean_signature = str(sign)
|
||||
|
||||
if os.path.isfile(str(path)):
|
||||
pass
|
||||
else:
|
||||
if not os.path.isfile(str(path)):
|
||||
self.app.logger.warning("File does not exist : " + str(path))
|
||||
flash('The file path for this image :' + str(path) + " wasn't found. Please try again.", 'error')
|
||||
return False
|
||||
else:
|
||||
self.app.logger.debug("Printing file from " + str(path))
|
||||
|
||||
|
||||
|
||||
try:
|
||||
self.app.logger.debug("Resizing the image")
|
||||
with Image.open(path) as im:
|
||||
|
||||
basewidth = 575
|
||||
img = Image.open(path)
|
||||
wpercent = (basewidth/float(img.size[0]))
|
||||
hsize = int((float(img.size[1])*float(wpercent)))
|
||||
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
|
||||
if img.height > 1000:
|
||||
flash("Image is too long, sorry ! Keep it below 500×1000 pixels.",'error')
|
||||
return False
|
||||
img.save(path)
|
||||
except Exception as e:
|
||||
flash(str(e))
|
||||
self.app.logger.error(str(e))
|
||||
|
||||
try:
|
||||
self.printer.open(self.usb_args)
|
||||
self.printer.textln("> " + clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||
self.printer.image(path)
|
||||
self.printer.cut()
|
||||
self.printer.close()
|
||||
self.app.logger.debug("Printed an image : " + str(path))
|
||||
return True
|
||||
except Exception as e:
|
||||
self.printer.close()
|
||||
return e
|
||||
else:
|
||||
self.app.logging.debug("Printed an image : " + str(path))
|
||||
flash(str(e),'error')
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user