1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-09-13 09:09:58 +00:00

Merge noio/python-escpos with latest google code

Merge branch 'master' of https://github.com/noio/python-escpos

Conflicts:
	escpos/__init__.py
	escpos/escpos.py
	escpos/exceptions.py
	escpos/printer.py
This commit is contained in:
Davis Goglin
2013-08-22 08:37:10 -07:00
5 changed files with 140 additions and 15 deletions

View File

@@ -9,6 +9,8 @@
import Image
import qrcode
import time
import os
import operator
from constants import *
from exceptions import *
@@ -51,8 +53,44 @@ class Escpos:
buffer = ""
cont = 0
def fullimage(self, img, max_height=860, width=512, histeq=True):
""" Resizes and prints an arbitrarily sized image """
if isinstance(img, Image.Image):
im = img.convert("RGB")
else:
im = Image.open(img).convert("RGB")
def _convert_image(self, im):
if histeq:
# Histogram equaliztion
h = im.histogram()
lut = []
for b in range(0, len(h), 256):
# step size
step = reduce(operator.add, h[b:b+256]) / 255
# create equalization lookup table
n = 0
for i in range(256):
lut.append(n / step)
n = n + h[i+b]
im = im.point(lut)
ratio = float(width) / im.size[0]
newheight = int(ratio * im.size[1])
# Resize the image
im = im.resize((width, newheight), Image.ANTIALIAS)
if im.size[1] > max_height:
im = im.crop((0, 0, im.size[0], max_height))
# Divide into bands
bandsize = 255
current = 0
while current < im.size[1]:
self.image(im.crop((0, current, width, min(im.size[1], current + bandsize))))
current += bandsize
def image(self, img):
""" Parse image and prepare it to a printable format """
pixels = []
pix_line = ""
@@ -61,9 +99,13 @@ class Escpos:
switch = 0
img_size = [ 0, 0 ]
if isinstance(img, Image.Image):
im = img.convert("RGB")
else:
im = Image.open(img).convert("RGB")
if im.size[0] > 512:
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
print "WARNING: Image is wider than 512 and could be truncated at print time "
if im.size[1] > 255:
raise ImageSizeError()
@@ -99,15 +141,6 @@ class Escpos:
self._print_image(pix_line, img_size)
def image(self,path_img):
""" Open image file """
im_open = Image.open(path_img)
im = im_open.convert("RGB")
# Convert the RGB image in printable image
self._convert_image(im)
def qr(self,text):
""" Print QR Code for the provided string """
qr_code = qrcode.QRCode(version=4, box_size=4, border=1)
@@ -118,7 +151,6 @@ class Escpos:
# Convert the RGB image in printable image
self._convert_image(im)
def barcode(self, code, bc, width, height, pos, font):
""" Print Barcode """
# Align Bar Code()
@@ -226,7 +258,6 @@ class Escpos:
elif align.upper() == "LEFT":
self._raw(TXT_ALIGN_LT)
def cut(self, mode=''):
""" Cut paper """
# Fix the size between last line and cut

View File

@@ -14,6 +14,9 @@ class Error(Exception):
def __str__(self):
return self.msg
class NotFoundError(Error):
""" Device wasn't found (not plugged in) """
# Result/Exit codes
# 0 = success
# 10 = No Barcode type defined

View File

@@ -38,7 +38,7 @@ class Usb(Escpos):
""" Search device on USB tree and set is as escpos device """
self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct)
if self.device is None:
print "Cable isn't plugged in"
raise NotFoundError("Device not found or cable not plugged in.")
if self.device.is_kernel_driver_active(0):
try:
@@ -137,7 +137,6 @@ class Network(Escpos):
self.device.close()
class File(Escpos):
""" Define Generic file printer """