2016-04-03 07:36:54 +00:00
|
|
|
#!/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>`_
|
2017-01-29 23:39:43 +00:00
|
|
|
:license: MIT
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2017-05-23 13:13:28 +00:00
|
|
|
import pytest
|
2016-04-03 07:36:54 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
2017-05-23 13:13:28 +00:00
|
|
|
import escpos.printer as printer
|
|
|
|
from escpos.exceptions import ImageWidthError
|
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
# Raster format print
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_black() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing solid black bit image (raster)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.image("test/resources/canvas_black.png", impl="bitImageRaster")
|
|
|
|
assert instance.output == b"\x1dv0\x00\x01\x00\x01\x00\x80"
|
2016-04-03 07:36:54 +00:00
|
|
|
# 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")
|
2021-10-30 16:15:22 +00:00
|
|
|
assert instance.output == b"\x1dv0\x00\x01\x00\x01\x00\x80"
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_white() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing solid white bit image (raster)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.image("test/resources/canvas_white.png", impl="bitImageRaster")
|
|
|
|
assert instance.output == b"\x1dv0\x00\x01\x00\x01\x00\x00"
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_both() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing black/white bit image (raster)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.image("test/resources/black_white.png", impl="bitImageRaster")
|
|
|
|
assert instance.output == b"\x1dv0\x00\x01\x00\x02\x00\xc0\x00"
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 22:09:20 +00:00
|
|
|
def test_bit_image_transparent() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing black/transparent bit image (raster)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.image("test/resources/black_transparent.png", impl="bitImageRaster")
|
|
|
|
assert instance.output == b"\x1dv0\x00\x01\x00\x02\x00\xc0\x00"
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
# Column format print
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_colfmt_black() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing solid black bit image (column format)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.image("test/resources/canvas_black.png", impl="bitImageColumn")
|
|
|
|
assert instance.output == b"\x1b3\x10\x1b*!\x01\x00\x80\x00\x00\x0a\x1b2"
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_colfmt_white() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing solid white bit image (column format)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.image("test/resources/canvas_white.png", impl="bitImageColumn")
|
|
|
|
assert instance.output == b"\x1b3\x10\x1b*!\x01\x00\x00\x00\x00\x0a\x1b2"
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_colfmt_both() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing black/white bit image (column format)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_bit_image_colfmt_transparent() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing black/transparent bit image (column format)
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
# Graphics print
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_graphics_black() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing solid black graphics
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_graphics_white() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing solid white graphics
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_graphics_both() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing black/white graphics
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_graphics_transparent() -> None:
|
2016-04-03 07:36:54 +00:00
|
|
|
"""
|
|
|
|
Test printing black/transparent graphics
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2016-08-07 11:04:36 +00:00
|
|
|
|
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_large_graphics() -> None:
|
2016-08-07 11:04:36 +00:00
|
|
|
"""
|
|
|
|
Test whether 'large' graphics that induce a fragmentation are handled correctly.
|
|
|
|
"""
|
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
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"
|
|
|
|
)
|
2017-05-23 13:13:28 +00:00
|
|
|
|
|
|
|
|
2017-08-31 07:25:35 +00:00
|
|
|
@pytest.fixture
|
2023-12-16 21:02:24 +00:00
|
|
|
def dummy_with_width() -> printer.Dummy:
|
2017-05-23 13:13:28 +00:00
|
|
|
instance = printer.Dummy()
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.profile.profile_data = {"media": {"width": {"pixels": 384}}}
|
2017-08-31 07:25:35 +00:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_width_too_large(dummy_with_width: printer.Dummy) -> None:
|
2017-08-31 07:25:35 +00:00
|
|
|
"""
|
|
|
|
Test printing an image that is too large in width.
|
|
|
|
"""
|
|
|
|
instance = dummy_with_width
|
2017-05-23 13:13:28 +00:00
|
|
|
|
|
|
|
with pytest.raises(ImageWidthError):
|
|
|
|
instance.image(Image.new("RGB", (385, 200)))
|
|
|
|
|
2017-08-31 07:25:35 +00:00
|
|
|
instance.image(Image.new("RGB", (384, 200)))
|
|
|
|
|
|
|
|
|
2023-12-16 21:02:24 +00:00
|
|
|
def test_center_image(dummy_with_width: printer.Dummy) -> None:
|
2017-08-31 07:25:35 +00:00
|
|
|
instance = dummy_with_width
|
|
|
|
|
|
|
|
with pytest.raises(ImageWidthError):
|
|
|
|
instance.image(Image.new("RGB", (385, 200)), center=True)
|
|
|
|
|
|
|
|
instance.image(Image.new("RGB", (384, 200)), center=True)
|