apply fixes, mainly to whitespace ( patch by @patkan in #128 )

This commit is contained in:
Michael Billington 2016-04-13 21:27:51 +10:00
parent 44c79eaf11
commit a0d8689141
5 changed files with 40 additions and 8 deletions

View File

@ -49,7 +49,7 @@ 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"):
""" Print an image
:param img_source: PIL image or filename to load: `jpg`, `gif`, `png` or `bmp`
@ -78,12 +78,11 @@ class Escpos(object):
if impl == "bitImageColumn":
# ESC *, column format bit image
density_byte = (1 if high_density_horizontal else 0) + (32 if high_density_vertical else 0)
header = ESC + b"*" + six.int2byte(density_byte) + self._int_low_high( im.width, 2 )
outp = []
outp.append(ESC + b"3" + six.int2byte(16)) # Adjust line-feed size
header = ESC + b"*" + six.int2byte(density_byte) + self._int_low_high(im.width, 2)
outp = [ESC + b"3" + six.int2byte(16)] # Adjust line-feed size
for blob in im.to_column_format(high_density_vertical):
outp.append(header + blob + b"\n")
outp.append(ESC + b"2"); # Reset line-feed size
outp.append(ESC + b"2") # Reset line-feed size
self._raw(b''.join(outp))
def _image_send_graphics_data(self, m, fn, data):

View File

@ -10,6 +10,7 @@ This module contains the image format handler :py:class:`EscposImage`.
from PIL import Image, ImageOps
class EscposImage(object):
"""
Load images in, and output ESC/POS formats.
@ -64,7 +65,7 @@ class EscposImage(object):
_, height_pixels = self._im.size
return height_pixels
def to_column_format(self, high_density_vertical = True):
def to_column_format(self, high_density_vertical=True):
"""
Extract slices of an image as equal-sized blobs of column-format data.

View File

@ -7,9 +7,15 @@
:license: GNU GPL v3
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import escpos.printer as printer
from PIL import Image
# Raster format print
def test_bit_image_black():
"""
@ -24,6 +30,7 @@ def test_bit_image_black():
instance.image(im, impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\x80')
def test_bit_image_white():
"""
Test printing solid white bit image (raster)
@ -32,6 +39,7 @@ def test_bit_image_white():
instance.image('test/resources/canvas_white.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\x00')
def test_bit_image_both():
"""
Test printing black/white bit image (raster)
@ -40,6 +48,7 @@ def test_bit_image_both():
instance.image('test/resources/black_white.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x02\x00\xc0\x00')
def test_bit_image_transparent():
"""
Test printing black/transparent bit image (raster)
@ -48,6 +57,7 @@ def test_bit_image_transparent():
instance.image('test/resources/black_transparent.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x02\x00\xc0\x00')
# Column format print
def test_bit_image_colfmt_black():
"""
@ -57,6 +67,7 @@ def test_bit_image_colfmt_black():
instance.image('test/resources/canvas_black.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x01\x00\x80\x00\x00\x0a\x1b2')
def test_bit_image_colfmt_white():
"""
Test printing solid white bit image (column format)
@ -65,6 +76,7 @@ def test_bit_image_colfmt_white():
instance.image('test/resources/canvas_white.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x01\x00\x00\x00\x00\x0a\x1b2')
def test_bit_image_colfmt_both():
"""
Test printing black/white bit image (column format)
@ -73,6 +85,7 @@ def test_bit_image_colfmt_both():
instance.image('test/resources/black_white.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x02\x00\x80\x00\x00\x80\x00\x00\x0a\x1b2')
def test_bit_image_colfmt_transparent():
"""
Test printing black/transparent bit image (column format)
@ -81,6 +94,7 @@ def test_bit_image_colfmt_transparent():
instance.image('test/resources/black_transparent.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x02\x00\x80\x00\x00\x80\x00\x00\x0a\x1b2')
# Graphics print
def test_graphics_black():
"""
@ -90,6 +104,7 @@ def test_graphics_black():
instance.image('test/resources/canvas_black.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x80\x1d(L\x02\x0002')
def test_graphics_white():
"""
Test printing solid white graphics
@ -98,6 +113,7 @@ def test_graphics_white():
instance.image('test/resources/canvas_white.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x00\x1d(L\x02\x0002')
def test_graphics_both():
"""
Test printing black/white graphics
@ -106,6 +122,7 @@ def test_graphics_both():
instance.image('test/resources/black_white.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002')
def test_graphics_transparent():
"""
Test printing black/transparent graphics

View File

@ -16,6 +16,7 @@ from nose.tools import raises
import escpos.printer as printer
from escpos.constants import QR_ECLEVEL_H, QR_MODEL_1
def test_defaults():
"""Test QR code with defaults"""
instance = printer.Dummy()
@ -24,12 +25,14 @@ def test_defaults():
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
def test_empty():
"""Test QR printing blank code"""
instance = printer.Dummy()
instance.qr("", native=True)
assert(instance.output == b'')
def test_ec():
"""Test QR error correction setting"""
instance = printer.Dummy()
@ -38,6 +41,7 @@ def test_ec():
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
def test_size():
"""Test QR box size"""
instance = printer.Dummy()
@ -46,6 +50,7 @@ def test_size():
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
def test_model():
"""Test QR model"""
instance = printer.Dummy()
@ -54,24 +59,28 @@ def test_model():
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
@raises(ValueError)
def test_invalid_ec():
"""Test invalid QR error correction"""
instance = printer.Dummy()
instance.qr("1234", native=True, ec=-1)
@raises(ValueError)
def test_invalid_size():
"""Test invalid QR size"""
instance = printer.Dummy()
instance.qr("1234", native=True, size=0)
@raises(ValueError)
def test_invalid_model():
"""Test invalid QR model"""
instance = printer.Dummy()
instance.qr("1234", native=True, model="Hello")
def test_image():
"""Test QR as image"""
instance = printer.Dummy()
@ -82,6 +91,7 @@ def test_image():
b'i(\x7f<\xa8A \xd8]\'\xc4]y\xf8]E\x80Ar\x94\x7fR@\x00\x00\x00'
assert(instance.output == expected)
@raises(ValueError)
def test_image_invalid_model():
"""Test unsupported QR model as image"""

View File

@ -10,6 +10,7 @@ converted to ESC/POS column & raster formats.
from escpos.image import EscposImage
def test_image_black():
"""
Test rendering solid black image
@ -17,6 +18,7 @@ def test_image_black():
for img_format in ['png', 'jpg', 'gif']:
_load_and_check_img('canvas_black.' + img_format, 1, 1, b'\x80', [b'\x80'])
def test_image_black_transparent():
"""
Test rendering black/transparent image
@ -24,6 +26,7 @@ def test_image_black_transparent():
for img_format in ['png', 'gif']:
_load_and_check_img('black_transparent.' + img_format, 2, 2, b'\xc0\x00', [b'\x80\x80'])
def test_image_black_white():
"""
Test rendering black/white image
@ -31,6 +34,7 @@ def test_image_black_white():
for img_format in ['png', 'jpg', 'gif']:
_load_and_check_img('black_white.' + img_format, 2, 2, b'\xc0\x00', [b'\x80\x80'])
def test_image_white():
"""
Test rendering solid white image
@ -38,6 +42,7 @@ def test_image_white():
for img_format in ['png', 'jpg', 'gif']:
_load_and_check_img('canvas_white.' + img_format, 1, 1, b'\x00', [b'\x00'])
def _load_and_check_img(filename, width_expected, height_expected, raster_format_expected, column_format_expected):
"""
Load an image, and test whether raster & column formatted output, sizes, etc match expectations.