From 645ea69c369785e5df4131866d72b09c200c37ba Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Tue, 03 May 2016 17:26:17 +0000
Subject: [PATCH] Add support for image printing

---
 test_image.py  |   14 ++++++++++++++
 axiohm.py      |   44 ++++++++++++++++++++++++++++++++++++++++++++
 test_image.png |    0 
 3 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/axiohm.py b/axiohm.py
index 1dba9ed..cc141b3 100644
--- a/axiohm.py
+++ b/axiohm.py
@@ -53,6 +53,11 @@
 	PITCH_STANDARD = 0
 	PITCH_COMPRESSED = 1
 	
+	IMAGE_MODE_8DOT_SINGLE = 0
+	IMAGE_MODE_8DOT_DOUBLE = 1
+	IMAGE_MODE_24DOT_SINGLE = 32
+	IMAGE_MODE_24DOT_DOUBLE = 33
+	
 	def __init__(self, **kwargs):
 		self.serial = serial.Serial(**kwargs)
 		self.currentStation = self.STATION_RECEIPT
@@ -322,6 +327,45 @@
 				self.moveAbsolute(startingPosition)
 			self.printLineUnicode(''.join([line[column] for line in lines]))
 	
+	# IMAGES
+	
+	def selectLogo(self, id):
+		self.serial.write("\x1d\x23" + chr(id % 256))
+	
+	def printLogo(self, mode = 0):
+		self.serial.write("\x1d\x2f" + chr(mode % 4))
+	
+	def printImage(self, image, modeNumber = 33):
+		assert self.currentStation == self.STATION_RECEIPT, \
+			'Printing images on slip station is not supported yet'
+		
+		# Pillow library image
+		image = image.convert("1")
+		from bitstring import BitArray
+		bits = BitArray(bytes=image.tobytes())
+		
+		mode = {
+			self.IMAGE_MODE_8DOT_SINGLE:  { "height": 8,  "width": 288 },
+			self.IMAGE_MODE_8DOT_DOUBLE:  { "height": 8,  "width": 576 },
+			self.IMAGE_MODE_24DOT_SINGLE: { "height": 24, "width": 288 },
+			self.IMAGE_MODE_24DOT_DOUBLE: { "height": 24, "width": 576 },
+		}[modeNumber]
+		
+		if image.height % mode['height'] > 0:
+			bits.append(BitArray(length = image.width * (mode['height'] - image.height % mode['height'])))
+		
+		for startLine in xrange(0, image.height, mode['height']):
+			printData = b''
+			start = startLine * image.width
+			end = start + image.width * mode['height']
+			for x in xrange(0, min(image.width, mode['width'])):
+				printData += ( bits[start + x : end : image.width].bytes )
+			
+			length = int(len(printData) / (mode['height'] / 8))
+			self.serial.write("\x1b\x2a" + chr(modeNumber) + chr(length % 256) + chr(int(length / 256)))
+			self.serial.write(printData)
+			self.serial.write("\r\n")
+	
 	# SLIP
 	
 	def waitForSlip(self):
diff --git a/test_image.png b/test_image.png
new file mode 100644
index 0000000..8e31b40
--- /dev/null
+++ b/test_image.png
Binary files differ
diff --git a/test_image.py b/test_image.py
new file mode 100755
index 0000000..4dc7c44
--- /dev/null
+++ b/test_image.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from axiohm import Axiohm
+import sys
+
+from PIL import Image
+
+printer = Axiohm(port = '/dev/ttyS0', baudrate = 19200, xonxoff = True)
+printer.reset()
+printer.selectReceipt()
+
+image = Image.open('test_image.png')
+printer.printImage(image, printer.IMAGE_MODE_24DOT_DOUBLE)

--
Gitblit v1.9.1