Fixing the creation of raster files, closes #1

This commit is contained in:
Philipp Klaus
2016-02-12 21:43:12 +01:00
parent a3b148f161
commit 9245da27f6
2 changed files with 16 additions and 7 deletions

View File

@@ -35,7 +35,12 @@ def main():
logging.basicConfig(level=args.loglevel)
qlr = BrotherQLRaster(args.model)
device_pixel_width = qlr.get_pixel_width()
im = Image.open(args.image)
hsize = int(im.size[1] / im.size[0] * device_pixel_width)
im = im.resize((device_pixel_width, hsize), Image.ANTIALIAS)
im = im.convert("L")
arr = np.asarray(im, dtype=np.uint8)
arr.flags.writeable = True
@@ -44,7 +49,7 @@ def main():
arr[white_idx] = 1
arr[black_idx] = 0
qlr = BrotherQLRaster(args.model)
qlr.set_mode()
qlr.set_clear_command_buffer()
qlr.set_initialize()

View File

@@ -124,19 +124,23 @@ class BrotherQLRaster(object):
self.data += b'\x4D'
self.data += bytes([compression << 1])
def set_raster_data(self, np_array):
""" np_array: numpy array of 1-bit values """
np_array = np.fliplr(np_array)
def get_pixel_width(self):
try:
nbpr = number_bytes_per_row[self.model]
except:
nbpr = number_bytes_per_row['default']
return nbpr*8
def set_raster_data(self, np_array):
""" np_array: numpy array of 1-bit values """
np_array = np.fliplr(np_array)
logger.info("raster_image_size: {1}x{0}".format(*np_array.shape))
if np_array.shape[1] != self.get_pixel_width():
fmt = 'Wrong pixel width: {}, expected {}'
raise BrotherQLRasterError(fmt.format(np_array.shape[0], self.get_pixel_width()))
for row in np_array:
self.data += b'\x67\x00'
row = bytes(np.packbits(row))
if len(row) != nbpr:
fmt = 'Wrong number of bytes per row: {}, expected {}'
raise BrotherQLRasterError(fmt.format(len(row), nbpr))
if self._compression:
row = packbits.encode(row)
self.data += bytes([len(row)])