Removing the numpy dependency
This commit is contained in:
@@ -90,12 +90,7 @@ def create_label(qlr, image, label_size, threshold=70, cut=True, **kwargs):
|
|||||||
im = new_im
|
im = new_im
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Label kind %s not implemented yet." % label_specs['kind'])
|
raise NotImplementedError("Label kind %s not implemented yet." % label_specs['kind'])
|
||||||
arr = np.asarray(im, dtype=np.uint8)
|
im = im.point(lambda p: p > threshold and 255, mode="1")
|
||||||
arr.flags.writeable = True
|
|
||||||
white_idx = arr[:,:] < threshold * 255./100.
|
|
||||||
black_idx = arr[:,:] >= threshold * 255./100.
|
|
||||||
arr[white_idx] = 1
|
|
||||||
arr[black_idx] = 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
qlr.add_switch_mode()
|
qlr.add_switch_mode()
|
||||||
@@ -139,7 +134,7 @@ def create_label(qlr, image, label_size, threshold=70, cut=True, **kwargs):
|
|||||||
qlr.add_compression(True)
|
qlr.add_compression(True)
|
||||||
except BrotherQLUnsupportedCmd:
|
except BrotherQLUnsupportedCmd:
|
||||||
pass
|
pass
|
||||||
qlr.add_raster_data(arr)
|
qlr.add_raster_data(im)
|
||||||
qlr.add_print()
|
qlr.add_print()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
+18
-9
@@ -5,7 +5,7 @@ import struct
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import packbits
|
import packbits
|
||||||
import numpy as np
|
from PIL import Image
|
||||||
|
|
||||||
from .devicedependent import models, \
|
from .devicedependent import models, \
|
||||||
min_max_feed, \
|
min_max_feed, \
|
||||||
@@ -168,16 +168,25 @@ class BrotherQLRaster(object):
|
|||||||
nbpr = number_bytes_per_row['default']
|
nbpr = number_bytes_per_row['default']
|
||||||
return nbpr*8
|
return nbpr*8
|
||||||
|
|
||||||
def add_raster_data(self, np_array):
|
def add_raster_data(self, image):
|
||||||
""" np_array: numpy array of 1-bit values """
|
""" image: Pillow Image() """
|
||||||
np_array = np.fliplr(np_array)
|
logger.info("raster_image_size: {0}x{1}".format(*image.size))
|
||||||
logger.info("raster_image_size: {1}x{0}".format(*np_array.shape))
|
image = image.transpose(Image.FLIP_LEFT_RIGHT)
|
||||||
if np_array.shape[1] != self.get_pixel_width():
|
image = image.convert("1")
|
||||||
|
if image.size[0] != self.get_pixel_width():
|
||||||
fmt = 'Wrong pixel width: {}, expected {}'
|
fmt = 'Wrong pixel width: {}, expected {}'
|
||||||
raise BrotherQLRasterError(fmt.format(np_array.shape[1], self.get_pixel_width()))
|
raise BrotherQLRasterError(fmt.format(image.size[0], self.get_pixel_width()))
|
||||||
for row in np_array:
|
frame = bytes(image.tobytes(encoder_name='raw'))
|
||||||
|
# The above command directly returns the 1-bit image packed
|
||||||
|
# into bits. (The cast to bytes is needed for Py2 compatibility.)
|
||||||
|
frame = bytes([2**8 + ~byte for byte in frame]) # invert b/w
|
||||||
|
frame_len = len(frame)
|
||||||
|
row_len = image.size[0]//8
|
||||||
|
start = 0
|
||||||
|
while start + row_len < frame_len:
|
||||||
|
row = frame[start:start+row_len]
|
||||||
|
start += row_len
|
||||||
self.data += b'\x67\x00' # g 0x00
|
self.data += b'\x67\x00' # g 0x00
|
||||||
row = bytes(np.packbits(row))
|
|
||||||
if self._compression:
|
if self._compression:
|
||||||
row = packbits.encode(row)
|
row = packbits.encode(row)
|
||||||
self.data += bytes([len(row)])
|
self.data += bytes([len(row)])
|
||||||
|
|||||||
Reference in New Issue
Block a user