Added ImageWidthError and its implementation (#226)

* Added ImageWidthError and its implementation

* Added unit tests for ImageWidthError

* Parse max_width to int before compare
This commit is contained in:
Romain Porte 2017-05-23 15:13:28 +02:00 committed by Patrick Kanzler
parent 5bf2636753
commit c4dd4f2960
3 changed files with 51 additions and 1 deletions

View File

@ -34,6 +34,7 @@ from .constants import TXT_STYLE
from .exceptions import BarcodeTypeError, BarcodeSizeError, TabPosError
from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError
from .exceptions import ImageWidthError
from .magicencode import MagicEncode
@ -99,6 +100,17 @@ class Escpos(object):
"""
im = EscposImage(img_source)
try:
max_width = int(self.profile.profile_data['media']['width']['pixels'])
if im.width > max_width:
raise ImageWidthError('{} > {}'.format(im.width, max_width))
except KeyError:
# If the printer's pixel width is not known, print anyways...
pass
except ValueError:
# If the max_width cannot be converted to an int, print anyways...
pass
if im.height > fragment_height:
fragments = im.split(fragment_height)
for fragment in fragments:

View File

@ -8,6 +8,7 @@ Result/Exit codes:
- `20` = Barcode size values are out of range :py:exc:`~escpos.exceptions.BarcodeSizeError`
- `30` = Barcode text not supplied :py:exc:`~escpos.exceptions.BarcodeCodeError`
- `40` = Image height is too large :py:exc:`~escpos.exceptions.ImageSizeError`
- `41` = Image width is too large :py:exc:`~escpos.exceptions.ImageWidthError`
- `50` = No string supplied to be printed :py:exc:`~escpos.exceptions.TextError`
- `60` = Invalid pin to send Cash Drawer pulse :py:exc:`~escpos.exceptions.CashDrawerError`
- `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError`
@ -104,6 +105,20 @@ class ImageSizeError(Error):
return "Image height is longer than 255px and can't be printed ({msg})".format(msg=self.msg)
class ImageWidthError(Error):
""" Image width is too large.
The return code for this exception is `41`.
"""
def __init__(self, msg=""):
Error.__init__(self, msg)
self.msg = msg
self.resultcode = 41
def __str__(self):
return "Image width is too large ({msg})".format(msg=self.msg)
class TextError(Error):
""" Text string must be supplied to the `text()` method.

View File

@ -12,9 +12,13 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import escpos.printer as printer
import pytest
from PIL import Image
import escpos.printer as printer
from escpos.exceptions import ImageWidthError
# Raster format print
def test_bit_image_black():
@ -139,3 +143,22 @@ def test_large_graphics():
instance = printer.Dummy()
instance.image('test/resources/black_white.png', impl="bitImageRaster", fragment_height=1)
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\xc0\x1dv0\x00\x01\x00\x01\x00\x00')
def test_width_too_large():
"""
Test printing an image that is too large in width.
"""
instance = printer.Dummy()
instance.profile.profile_data = {
'media': {
'width': {
'pixels': 384
}
}
}
with pytest.raises(ImageWidthError):
instance.image(Image.new("RGB", (385, 200)))
instance.image(Image.new("RGB", (384, 200)))