diff --git a/src/main.py b/src/main.py index 33d89e4..9f20795 100644 --- a/src/main.py +++ b/src/main.py @@ -173,9 +173,13 @@ def web_print_sms(): "No signature found for this print, using default signature : %s ", str(e) ) sign = configuration_file["defaults"]["signature"] + try: + do_cut = bool(request.form["cut"]) + except werkzeug.exceptions.BadRequestKeyError: + do_cut = False try: - web.print_sms(txt, sign) + web.print_sms(txt, sign, do_cut) except RuntimeError as e: app.logger.error("Whoops, we could not print an SMS because : %s ", str(e)) flash("Whoops, we could not print an SMS because :" + str(e), "error") @@ -219,9 +223,20 @@ def web_print_img(): flash("Submitted file has no filename !", "error") return redirect(url_for("index")) + try: + do_cut = bool(request.form["cut"]) + except werkzeug.exceptions.BadRequestKeyError: + do_cut = False + + try: + process_image = bool(request.form["process"]) + except werkzeug.exceptions.BadRequestKeyError: + process_image = False + + try: app.logger.debug("Sending the image to the printer.") - web.print_image(file, sign) + web.print_image(file, sign, do_cut, process_image) except RuntimeError as e: app.logger.error("The image could not be printed because : %s ", str(e)) flash("The image could not be printed because : " + str(e), "error") @@ -266,9 +281,16 @@ def api_print_sms(): "No signature found for this print, using default signature. : %s", str(e) ) sign = configuration_file["defaults"]["signature"] + + try: + do_cut = bool(request.form["cut"]) + except werkzeug.exceptions.BadRequestKeyError: + do_cut = False + + try: # comment: We try to print the SMS - web.print_sms(txt, sign) + web.print_sms(txt, sign, do_cut) except RuntimeError as e: return str(e), 500 # end try @@ -301,9 +323,19 @@ def api_print_image(): app.logger.error("Submitted file has no filename !") return "Submitted file has no filename !", 400 + try: + do_cut = bool(request.form["cut"]) + except werkzeug.exceptions.BadRequestKeyError: + do_cut = False + + try: + process_image = bool(request.form["process"]) + except werkzeug.exceptions.BadRequestKeyError: + process_image = False + try: app.logger.debug("Sending the image to the printer.") - web.print_image(file, sign) + web.print_image(file, sign, do_cut, process_image) except RuntimeError as e: return str(e), 500 diff --git a/src/printer.py b/src/printer.py index 66f81e5..ee337cb 100644 --- a/src/printer.py +++ b/src/printer.py @@ -460,22 +460,35 @@ class BrotherPrinter(Printer): """Print text after converting it to an image""" self._print_img(text_to_image(msg)) - def _print_img(self, data): + def _print_img(self, data, process=False): """ Print a raster image via a Brother QL printer """ self.ready = False + + if process: + try: + self.app.logger.debug("Proccessing the image") + processed_path = _process_image(self, data) + except RuntimeError as e: + self.app.logger.error( + "Error while processing the image, aborting print : %s", str(e) + ) + raise e + else: + processed_path = data + self.app.logger.warning("Not proccessing the image") try: selected_backend = guess_backend(self.printer_info.identifier) BACKEND_CLASS = backend_factory(selected_backend)['backend_class'] BACKEND_STRING_DESCR = self.printer_info.identifier - except Exception as e: + except Exception: self.app.logger.error("Couldn't guess the backend to use from the printer string descriptor") try: - im = Image.open(data) + im = Image.open(processed_path) qlr = BrotherQLRaster(self.printer_info.model) - create_label(qlr, im, self.printer_info.label_type, threshold=70, cut=False, rotate=0) + create_label(qlr, im, self.printer_info.label_type, cut=False, dither=True) # noinspection PyCallingNonCallable be = BACKEND_CLASS(BACKEND_STRING_DESCR) diff --git a/src/static/images/1x1.png b/src/static/images/1x1.png new file mode 100644 index 0000000..ef90d30 Binary files /dev/null and b/src/static/images/1x1.png differ diff --git a/src/static/images/calibration.png b/src/static/images/calibration.png new file mode 100644 index 0000000..066baf1 Binary files /dev/null and b/src/static/images/calibration.png differ diff --git a/src/templates/index.html b/src/templates/index.html index 4a7be1e..b20aeb7 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -6,13 +6,13 @@

Print a short message

- +
-
+
- +

@@ -28,11 +28,17 @@

-
-
- +
+
+ +
+
+ +

diff --git a/src/web.py b/src/web.py index 90f88ea..173ae06 100644 --- a/src/web.py +++ b/src/web.py @@ -16,7 +16,7 @@ class Web: self.print_queue = print_queue self.app = app - def print_sms(self, texte, sign: str) -> bool: + def print_sms(self, texte, sign: str, do_cut: bool) -> bool: """ Get text and a signature, prints the text and cuts after that. """ @@ -29,14 +29,18 @@ class Web: # are added back to back, another task could be # inserted between the two. sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign)) - cut = self.print_queue.enqueue(CutTask()) + self.app.logger.info("Added a SMS task at position %s", sms) + + if do_cut: + cut = self.print_queue.enqueue(CutTask()) + self.app.logger.info("Added a CUT task at position %s", cut) except Exception as e: self.app.logger.error(e) raise RuntimeError("Could not add SMS to queue, " + str(e)) from e - self.app.logger.info("Added two new tasks at position %s and %s", sms, cut) + return True - def print_image(self, image, sign: str) -> bool: + def print_image(self, image, sign: str, do_cut: bool, process_image: bool) -> bool: """ Get an image and a signature, prints the image and cuts after that. """ @@ -56,16 +60,17 @@ class Web: secure_filename(image.filename), ), signature=sign, - process=True, + process=process_image, ) ) + self.app.logger.info("Added a IMG task at position %s", img) - cut = self.print_queue.enqueue(CutTask()) + if do_cut: + cut = self.print_queue.enqueue(CutTask()) + self.app.logger.info("Added a CUT task at position %s", cut) except Exception as e: raise RuntimeError("Could not add IMG to queue" + str(e)) from e - self.app.logger.info("Added two new tasks at position %s and %s", img, cut) - return True # def login(self, username: str, password: str) -> bool: