1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-08-24 09:03:34 +00:00

add implementation of GS v 0, GS ( L and GS *.

- ported test cases for EscposImage class, copied over 1px and 2px test images from escpos-php
- added test cases over image print function
- updated QR tests to also include image output check
- updated CLI to match new image function options
This commit is contained in:
Michael Billington
2016-04-03 17:36:54 +10:00
parent 59afcf778f
commit b45afbb297
18 changed files with 416 additions and 71 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 B

115
test/test_function_image.py Normal file
View File

@@ -0,0 +1,115 @@
#!/usr/bin/env python
""" Image function tests- Check that image print commands are sent correctly.
:author: `Michael Billington <michael.billington@gmail.com>`_
:organization: `python-escpos <https://github.com/python-escpos>`_
:copyright: Copyright (c) 2016 `Michael Billington <michael.billington@gmail.com>`_
:license: GNU GPL v3
"""
import escpos.printer as printer
from PIL import Image
# Raster format print
def test_bit_image_black():
"""
Test printing solid black bit image (raster)
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_black.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\x80')
# Same thing w/ object created on the fly, rather than a filename
instance = printer.Dummy()
im = Image.new("RGB", (1, 1), (0, 0, 0))
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)
"""
instance = printer.Dummy()
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)
"""
instance = printer.Dummy()
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)
"""
instance = printer.Dummy()
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():
"""
Test printing solid black bit image (column format)
"""
instance = printer.Dummy()
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)
"""
instance = printer.Dummy()
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)
"""
instance = printer.Dummy()
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)
"""
instance = printer.Dummy()
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():
"""
Test printing solid black graphics
"""
instance = printer.Dummy()
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
"""
instance = printer.Dummy()
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
"""
instance = printer.Dummy()
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
"""
instance = printer.Dummy()
instance.image('test/resources/black_transparent.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002')

View File

@@ -12,70 +12,78 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from nose.tools import with_setup
from nose.tools import raises
import escpos.printer as printer
import os
from escpos.constants import QR_ECLEVEL_H, QR_MODEL_1
devfile = 'testfile'
def setup_testfile():
"""create a testfile as devfile"""
fhandle = open(devfile, 'a')
try:
os.utime(devfile, None)
finally:
fhandle.close()
def teardown_testfile():
"""destroy testfile again"""
os.remove(devfile)
@with_setup(setup_testfile, teardown_testfile)
def test_function_qr_defaults():
"""test QR code with defaults"""
instance = printer.File(devfile=devfile)
def test_defaults():
"""Test QR code with defaults"""
instance = printer.Dummy()
instance.qr("1234", native=True)
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0')
expected = b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
@with_setup(setup_testfile, teardown_testfile)
def test_function_qr_empty():
"""test QR printing blank code"""
instance = printer.File(devfile=devfile)
def test_empty():
"""Test QR printing blank code"""
instance = printer.Dummy()
instance.qr("", native=True)
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'')
assert(instance.output == b'')
@with_setup(setup_testfile, teardown_testfile)
def test_function_qr_ec():
"""test QR error correction setting"""
instance = printer.File(devfile=devfile)
def test_ec():
"""Test QR error correction setting"""
instance = printer.Dummy()
instance.qr("1234", native=True, ec=QR_ECLEVEL_H)
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0')
expected = b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
@with_setup(setup_testfile, teardown_testfile)
def test_function_qr_size():
"""test QR box size"""
instance = printer.File(devfile=devfile)
def test_size():
"""Test QR box size"""
instance = printer.Dummy()
instance.qr("1234", native=True, size=7)
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0')
expected = b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
@with_setup(setup_testfile, teardown_testfile)
def test_function_qr_model():
"""test QR model"""
instance = printer.File(devfile=devfile)
def test_model():
"""Test QR model"""
instance = printer.Dummy()
instance.qr("1234", native=True, model=QR_MODEL_1)
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0')
expected = b'\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d' \
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()
instance.qr("1", native=False, size=1)
expected = b'\x1d(LO\x000p0\x01\x011\x17\x00\x17\x00\x00\x00\x00\x7f]\xfcA' \
b'\x19\x04]it]et]ItA=\x04\x7fU\xfc\x00\x0c\x00y~t4\x7f =\xa84j\xd9\xf0' \
b'\x05\xd4\x90\x00i(\x7f<\xa8A \xd8]\'\xc4]y\xf8]E\x80Ar\x94\x7fR@\x00\x00' \
b'\x00\x1d(L\x02\x0002'
assert(instance.output == expected)
@raises(ValueError)
def test_image_invalid_model():
"""Test unsupported QR model as image"""
instance = printer.Dummy()
instance.qr("1234", native=False, model=QR_MODEL_1)

52
test/test_image.py Normal file
View File

@@ -0,0 +1,52 @@
#!/usr/bin/env python
""" Image tests- Check that images from different source formats are correctly
converted to ESC/POS column & raster formats.
:author: `Michael Billington <michael.billington@gmail.com>`_
:organization: `python-escpos <https://github.com/python-escpos>`_
:copyright: Copyright (c) 2016 `Michael Billington <michael.billington@gmail.com>`_
:license: GNU GPL v3
"""
from escpos.image import EscposImage
def test_image_black():
"""
Test rendering solid black image
"""
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
"""
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
"""
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
"""
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.
"""
im = EscposImage('test/resources/' + filename)
assert(im.width == width_expected)
assert(im.height == height_expected)
assert(im.to_raster_format() == raster_format_expected)
i = 0
for row in im.to_column_format(False):
assert(row == column_format_expected[i])
i = i + 1