add type annotations for escpos image handler (#599)

This commit is contained in:
Patrick Kanzler 2023-12-03 23:36:35 +01:00 committed by GitHub
parent 8274833255
commit 9a1699ab94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 12 deletions

View File

@ -3,6 +3,7 @@ name = python-escpos
url = https://github.com/python-escpos/python-escpos url = https://github.com/python-escpos/python-escpos
description = Python library to manipulate ESC/POS Printers description = Python library to manipulate ESC/POS Printers
long_description = file: README.rst long_description = file: README.rst
long_description_content_type = text/x-rst
license = MIT license = MIT
license_file = LICENSE license_file = LICENSE
author = python-escpos developers author = python-escpos developers

View File

@ -174,11 +174,11 @@ class Escpos(object):
def image( def image(
self, self,
img_source, img_source,
high_density_vertical=True, high_density_vertical: bool = True,
high_density_horizontal=True, high_density_horizontal: bool = True,
impl="bitImageRaster", impl: str = "bitImageRaster",
fragment_height=960, fragment_height: int = 960,
center=False, center: bool = False,
) -> None: ) -> None:
"""Print an image. """Print an image.

View File

@ -10,6 +10,7 @@ This module contains the image format handler :py:class:`EscposImage`.
import math import math
from typing import Union
from PIL import Image, ImageOps from PIL import Image, ImageOps
@ -22,7 +23,7 @@ class EscposImage(object):
PIL, rather than spend CPU cycles looping over pixels. 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. """Load in an image.
:param img_source: PIL.Image, or filename to load one from. :param img_source: PIL.Image, or filename to load one from.
@ -48,23 +49,23 @@ class EscposImage(object):
self._im = im.convert("1") self._im = im.convert("1")
@property @property
def width(self): def width(self) -> int:
"""Return width of image in pixels.""" """Return width of image in pixels."""
width_pixels, _ = self._im.size width_pixels, _ = self._im.size
return width_pixels return width_pixels
@property @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 width of image if you use 8 pixels per byte and 0-pad at the end."""
return (self.width + 7) >> 3 return (self.width + 7) >> 3
@property @property
def height(self): def height(self) -> int:
"""Height of image in pixels.""" """Height of image in pixels."""
_, height_pixels = self._im.size _, height_pixels = self._im.size
return height_pixels 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. """Extract slices of an image as equal-sized blobs of column-format data.
:param high_density_vertical: Printed line height in dots :param high_density_vertical: Printed line height in dots
@ -85,7 +86,7 @@ class EscposImage(object):
"""Convert image to raster-format binary.""" """Convert image to raster-format binary."""
return self._im.tobytes() 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. """Split an image into multiple fragments after fragment_height pixels.
:param fragment_height: height of fragment :param fragment_height: height of fragment
@ -102,7 +103,7 @@ class EscposImage(object):
fragments.append(self.img_original.crop(box)) fragments.append(self.img_original.crop(box))
return fragments return fragments
def center(self, max_width): def center(self, max_width: int) -> None:
"""Center image in place. """Center image in place.
:param: Maximum width in order to deduce x offset for centering :param: Maximum width in order to deduce x offset for centering