def test_split(): """ test whether the split-function works as expected """ im = EscposImage('test/resources/black_white.png') (upper_part, lower_part) = im.split(1) upper_part = EscposImage(upper_part) lower_part = EscposImage(lower_part) assert(upper_part.width == lower_part.width == 2) assert(upper_part.height == lower_part.height == 1) assert(upper_part.to_raster_format() == b'\xc0') assert(lower_part.to_raster_format() == b'\x00')
def test_split(): """ test whether the split-function works as expected """ im = EscposImage('test/resources/black_white.png') (upper_part, lower_part) = im.split(1) upper_part = EscposImage(upper_part) lower_part = EscposImage(lower_part) assert (upper_part.width == lower_part.width == 2) assert (upper_part.height == lower_part.height == 1) assert (upper_part.to_raster_format() == b'\xc0') assert (lower_part.to_raster_format() == b'\x00')
def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster", fragment_height=1024): """ Print an image You can select whether the printer should print in high density or not. The default value is high density. When printing in low density, the image will be stretched. Esc/Pos supplies several commands for printing. This function supports three of them. Please try to vary the implementations if you have any problems. For example the printer `IT80-002` will have trouble aligning images that are not printed in Column-mode. The available printing implementations are: * `bitImageRaster`: prints with the `GS v 0`-command * `graphics`: prints with the `GS ( L`-command * `bitImageColumn`: prints with the `ESC *`-command :param img_source: PIL image or filename to load: `jpg`, `gif`, `png` or `bmp` :param high_density_vertical: print in high density in vertical direction *default:* True :param high_density_horizontal: print in high density in horizontal direction *default:* True :param impl: choose image printing mode between `bitImageRaster`, `graphics` or `bitImageColumn` :param fragment_height: Images larger than this will be split into multiple fragments *default:* 1024 """ im = EscposImage(img_source) if im.height > fragment_height: fragments = im.split(fragment_height) for fragment in fragments: self.image(fragment, high_density_vertical=high_density_vertical, high_density_horizontal=high_density_horizontal, impl=impl, fragment_height=fragment_height) return if impl == "bitImageRaster": # GS v 0, raster format bit image density_byte = (0 if high_density_horizontal else 1) + (0 if high_density_vertical else 2) header = GS + b"v0" + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) + self._int_low_high(im.height, 2) self._raw(header + im.to_raster_format()) if impl == "graphics": # GS ( L raster format graphics img_header = self._int_low_high(im.width, 2) + self._int_low_high(im.height, 2) tone = b'0' colors = b'1' ym = six.int2byte(1 if high_density_vertical else 2) xm = six.int2byte(1 if high_density_horizontal else 2) header = tone + xm + ym + colors + img_header raster_data = im.to_raster_format() self._image_send_graphics_data(b'0', b'p', header + raster_data) self._image_send_graphics_data(b'0', b'2', b'') 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 = [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 self._raw(b''.join(outp))
def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster", fragment_height=960, center=False): """ Print an image You can select whether the printer should print in high density or not. The default value is high density. When printing in low density, the image will be stretched. Esc/Pos supplies several commands for printing. This function supports three of them. Please try to vary the implementations if you have any problems. For example the printer `IT80-002` will have trouble aligning images that are not printed in Column-mode. The available printing implementations are: * `bitImageRaster`: prints with the `GS v 0`-command * `graphics`: prints with the `GS ( L`-command * `bitImageColumn`: prints with the `ESC *`-command :param img_source: PIL image or filename to load: `jpg`, `gif`, `png` or `bmp` :param high_density_vertical: print in high density in vertical direction *default:* True :param high_density_horizontal: print in high density in horizontal direction *default:* True :param impl: choose image printing mode between `bitImageRaster`, `graphics` or `bitImageColumn` :param fragment_height: Images larger than this will be split into multiple fragments *default:* 960 :param center: Center image horizontally *default:* False """ im = EscposImage(img_source) try: max_width = int( self.profile.profile_data['media']['width']['pixels']) if im.width > max_width: raise ImageWidthError('{} > {}'.format(im.width, max_width)) if center: im.center(max_width) except KeyError: # If the printer's pixel width is not known, print anyways... pass except ValueError: # If the max_width cannot be converted to an int, print anyways... pass if im.height > fragment_height: fragments = im.split(fragment_height) for fragment in fragments: self.image(fragment, high_density_vertical=high_density_vertical, high_density_horizontal=high_density_horizontal, impl=impl, fragment_height=fragment_height) return if impl == "bitImageRaster": # GS v 0, raster format bit image density_byte = (0 if high_density_horizontal else 1) + (0 if high_density_vertical else 2) header = GS + b"v0" + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) +\ self._int_low_high(im.height, 2) self._raw(header + im.to_raster_format()) if impl == "graphics": # GS ( L raster format graphics img_header = self._int_low_high(im.width, 2) + self._int_low_high( im.height, 2) tone = b'0' colors = b'1' ym = six.int2byte(1 if high_density_vertical else 2) xm = six.int2byte(1 if high_density_horizontal else 2) header = tone + xm + ym + colors + img_header raster_data = im.to_raster_format() self._image_send_graphics_data(b'0', b'p', header + raster_data) self._image_send_graphics_data(b'0', b'2', b'') 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 = [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 self._raw(b''.join(outp))