create/reader: Removing remaining traces of the numpy package

This commit is contained in:
Philipp Klaus
2016-12-10 15:23:57 +01:00
parent bfad2148b0
commit 76f4677c12
2 changed files with 14 additions and 12 deletions
-1
View File
@@ -4,7 +4,6 @@ from __future__ import division
import sys, argparse, logging import sys, argparse, logging
import numpy as np
from PIL import Image from PIL import Image
from brother_ql.raster import BrotherQLRaster from brother_ql.raster import BrotherQLRaster
+14 -11
View File
@@ -8,6 +8,8 @@ import sys
from PIL import Image from PIL import Image
import numpy as np import numpy as np
from builtins import bytes
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
OPCODES = { OPCODES = {
@@ -118,6 +120,7 @@ def chunker(data, raise_exception=False):
returns: list of bytes objects returns: list of bytes objects
""" """
instructions = [] instructions = []
data = bytes(data)
while True: while True:
if len(data) == 0: break if len(data) == 0: break
try: try:
@@ -247,25 +250,25 @@ class BrotherQLReader(object):
if opcode_def[0] == 'compression': if opcode_def[0] == 'compression':
self.compression = payload[0] == 0x02 self.compression = payload[0] == 0x02
if opcode_def[0] == 'raster': if opcode_def[0] == 'raster':
rpl = payload[2:] # raster payload rpl = bytes(payload[2:]) # raster payload
index = 0
row = []
if self.compression: if self.compression:
row = bytes()
index = 0
while True: while True:
num = rpl[index] num = rpl[index]
if num & 0x80: if num & 0x80:
num = num - 0x100 num = num - 0x100
if num < 0: if num < 0:
num = -num + 1 num = -num + 1
for i in range(num): row.append(rpl[index+1]) row += bytes([rpl[index+1]] * num)
index += 2 index += 2
else: else:
num = num + 1 num = num + 1
for i in range(num): row.append(rpl[index+1+i]) row += rpl[index+1:index+1+num]
index += 1 + num index += 1 + num
if index >= len(rpl): break if index >= len(rpl): break
else: else:
row.append(list(rpl)) row = rpl
self.rows.append(row) self.rows.append(row)
if opcode_def[0] == 'media/quality': if opcode_def[0] == 'media/quality':
self.raster_no = struct.unpack('<L', payload[4:8])[0] self.raster_no = struct.unpack('<L', payload[4:8])[0]
@@ -274,11 +277,11 @@ class BrotherQLReader(object):
fmt = " media width: {}mm, media length: {}mm, raster no: {}dots" fmt = " media width: {}mm, media length: {}mm, raster no: {}dots"
logger.info(fmt.format(self.mwidth, self.mlength, self.raster_no)) logger.info(fmt.format(self.mwidth, self.mlength, self.raster_no))
if opcode_def[0] == 'print': if opcode_def[0] == 'print':
self.rows = [np.unpackbits(np.array(row, dtype=np.uint8)) for row in self.rows] size = (len(self.rows[0])*8, len(self.rows))
array = np.array(self.rows, dtype=np.uint8) data = bytes(b''.join(self.rows))
array = np.fliplr(array) data = bytes([2**8 + ~byte for byte in data]) # invert b/w
im = Image.fromarray(array) im = Image.frombytes("1", size, data, decoder_name='raw')
im = im.point(lambda x: 0 if x == 1 else 255, '1') # -> Monocolor and invert im = im.transpose(Image.FLIP_LEFT_RIGHT)
img_name = 'page{:04d}.png'.format(self.page) img_name = 'page{:04d}.png'.format(self.page)
im.save(img_name) im.save(img_name)
print('Page saved as {}'.format(img_name)) print('Page saved as {}'.format(img_name))