From 2ecf73074c166cea1da3249f521041f55b91b40c Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Mon, 1 Aug 2016 14:39:44 +0200 Subject: [PATCH] improve large image printing images longer than 1024 pixels will be split into multiple fragments. --- src/escpos/escpos.py | 14 +++++++++++++- src/escpos/image.py | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 081130a..c0df537 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -56,7 +56,8 @@ class Escpos(object): """ pass - def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster"): + def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster", + fragment_height=1024): """ Print an image You can select whether the printer should print in high density or not. The default value is high density. @@ -76,9 +77,20 @@ class Escpos(object): :param high_density_vertical: print in high density in vertical direction *default:* True :param high_density_horizontal: print in high density in horizontal direction *default:* True :param impl: choose image printing mode between `bitImageRaster`, `graphics` or `bitImageColumn` + :param fragment_height: Images larger than this will be split into multiple fragments *default:* 1024 """ im = EscposImage(img_source) + + if im.height > fragment_height: + fragments = im.split(fragment_height) + for fragment in fragments: + self.image(fragment, + high_density_vertical=high_density_vertical, + high_density_horizontal=high_density_horizontal, + impl=impl, + fragment_height=fragment_height) + return if impl == "bitImageRaster": # GS v 0, raster format bit image diff --git a/src/escpos/image.py b/src/escpos/image.py index 1180614..abf2e22 100644 --- a/src/escpos/image.py +++ b/src/escpos/image.py @@ -8,6 +8,12 @@ This module contains the image format handler :py:class:`EscposImage`. :license: GNU GPL v3 """ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import math from PIL import Image, ImageOps @@ -30,6 +36,9 @@ class EscposImage(object): else: img_original = Image.open(img_source) + # store image for eventual further processing (splitting) + self.img_original = img_original + # Convert to white RGB background, paste over white background # to strip alpha. img_original = img_original.convert('RGBA') @@ -88,3 +97,21 @@ class EscposImage(object): Convert image to raster-format binary """ return self._im.tobytes() + + def split(self, fragment_height): + """ + Split an image into multiple fragments after fragment_height pixels + + :param fragment_height: height of fragment + :return: list of PIL objects + """ + passes = int(math.ceil(self.height/fragment_height)) + fragments = [] + for n in range(0, passes): + left = 0 + right = self.width + upper = n * fragment_height + lower = min((n + 1) * fragment_height, self.height) + box = (left, upper, right, lower) + fragments.append(self.img_original.crop(box)) + return fragments