From 9a1699ab94b8dc506bc01d97e16e57b49c96092b Mon Sep 17 00:00:00 2001 From: Patrick Kanzler <4189642+patkan@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:36:35 +0100 Subject: [PATCH] add type annotations for escpos image handler (#599) --- setup.cfg | 1 + src/escpos/escpos.py | 10 +++++----- src/escpos/image.py | 15 ++++++++------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/setup.cfg b/setup.cfg index 44836c4..bcebf47 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,6 +3,7 @@ name = python-escpos url = https://github.com/python-escpos/python-escpos description = Python library to manipulate ESC/POS Printers long_description = file: README.rst +long_description_content_type = text/x-rst license = MIT license_file = LICENSE author = python-escpos developers diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index c0ab276..76066ec 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -174,11 +174,11 @@ class Escpos(object): def image( self, img_source, - high_density_vertical=True, - high_density_horizontal=True, - impl="bitImageRaster", - fragment_height=960, - center=False, + high_density_vertical: bool = True, + high_density_horizontal: bool = True, + impl: str = "bitImageRaster", + fragment_height: int = 960, + center: bool = False, ) -> None: """Print an image. diff --git a/src/escpos/image.py b/src/escpos/image.py index a5dbfea..70fae52 100644 --- a/src/escpos/image.py +++ b/src/escpos/image.py @@ -10,6 +10,7 @@ This module contains the image format handler :py:class:`EscposImage`. import math +from typing import Union from PIL import Image, ImageOps @@ -22,7 +23,7 @@ class EscposImage(object): PIL, rather than spend CPU cycles looping over pixels. """ - def __init__(self, img_source): + def __init__(self, img_source: Union[Image.Image, str]): """Load in an image. :param img_source: PIL.Image, or filename to load one from. @@ -48,23 +49,23 @@ class EscposImage(object): self._im = im.convert("1") @property - def width(self): + def width(self) -> int: """Return width of image in pixels.""" width_pixels, _ = self._im.size return width_pixels @property - def width_bytes(self): + def width_bytes(self) -> int: """Return width of image if you use 8 pixels per byte and 0-pad at the end.""" return (self.width + 7) >> 3 @property - def height(self): + def height(self) -> int: """Height of image in pixels.""" _, 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: bool = True): """Extract slices of an image as equal-sized blobs of column-format data. :param high_density_vertical: Printed line height in dots @@ -85,7 +86,7 @@ class EscposImage(object): """Convert image to raster-format binary.""" return self._im.tobytes() - def split(self, fragment_height): + def split(self, fragment_height: int): """Split an image into multiple fragments after fragment_height pixels. :param fragment_height: height of fragment @@ -102,7 +103,7 @@ class EscposImage(object): fragments.append(self.img_original.crop(box)) return fragments - def center(self, max_width): + def center(self, max_width: int) -> None: """Center image in place. :param: Maximum width in order to deduce x offset for centering