Merge pull request #16 from iegomez/master (w/o multiprocessing)
Speed up implementation
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import division
|
|||||||
import sys, argparse, logging
|
import sys, argparse, logging
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import PIL.ImageOps
|
||||||
|
|
||||||
from brother_ql.raster import BrotherQLRaster
|
from brother_ql.raster import BrotherQLRaster
|
||||||
from brother_ql.devicedependent import models, label_type_specs, ENDLESS_LABEL, DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL
|
from brother_ql.devicedependent import models, label_type_specs, ENDLESS_LABEL, DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL
|
||||||
@@ -100,6 +101,9 @@ def create_label(qlr, image, label_size, threshold=70, cut=True, **kwargs):
|
|||||||
new_im.paste(im, (device_pixel_width-im.size[0]-right_margin_dots, 0))
|
new_im.paste(im, (device_pixel_width-im.size[0]-right_margin_dots, 0))
|
||||||
im = new_im
|
im = new_im
|
||||||
|
|
||||||
|
im = PIL.ImageOps.invert(im)
|
||||||
|
|
||||||
|
threshold = 100.0 - threshold
|
||||||
threshold = min(255, max(0, int(threshold/100.0 * 255))) # from percent to pixel val
|
threshold = min(255, max(0, int(threshold/100.0 * 255))) # from percent to pixel val
|
||||||
im = im.point(lambda x: 0 if x < threshold else 255, mode="1")
|
im = im.point(lambda x: 0 if x < threshold else 255, mode="1")
|
||||||
|
|
||||||
|
|||||||
+11
-6
@@ -6,6 +6,7 @@ import logging
|
|||||||
|
|
||||||
import packbits
|
import packbits
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import io
|
||||||
|
|
||||||
from .devicedependent import models, \
|
from .devicedependent import models, \
|
||||||
min_max_feed, \
|
min_max_feed, \
|
||||||
@@ -19,6 +20,11 @@ from .devicedependent import models, \
|
|||||||
|
|
||||||
from . import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel, BrotherQLRasterError
|
from . import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel, BrotherQLRasterError
|
||||||
|
|
||||||
|
try:
|
||||||
|
from io import BytesIO
|
||||||
|
except: # Py2
|
||||||
|
from cStringIO import StringIO as BytesIO
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class BrotherQLRaster(object):
|
class BrotherQLRaster(object):
|
||||||
@@ -177,20 +183,19 @@ class BrotherQLRaster(object):
|
|||||||
fmt = 'Wrong pixel width: {}, expected {}'
|
fmt = 'Wrong pixel width: {}, expected {}'
|
||||||
raise BrotherQLRasterError(fmt.format(image.size[0], self.get_pixel_width()))
|
raise BrotherQLRasterError(fmt.format(image.size[0], self.get_pixel_width()))
|
||||||
frame = bytes(image.tobytes(encoder_name='raw'))
|
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)
|
frame_len = len(frame)
|
||||||
row_len = image.size[0]//8
|
row_len = image.size[0]//8
|
||||||
start = 0
|
start = 0
|
||||||
|
file_str = BytesIO()
|
||||||
while start + row_len <= frame_len:
|
while start + row_len <= frame_len:
|
||||||
row = frame[start:start+row_len]
|
row = frame[start:start+row_len]
|
||||||
start += row_len
|
start += row_len
|
||||||
self.data += b'\x67\x00' # g 0x00
|
file_str.write(b'\x67\x00')
|
||||||
if self._compression:
|
if self._compression:
|
||||||
row = packbits.encode(row)
|
row = packbits.encode(row)
|
||||||
self.data += bytes([len(row)])
|
file_str.write(bytes([len(row)]))
|
||||||
self.data += row
|
file_str.write(row)
|
||||||
|
self.data += file_str.getvalue()
|
||||||
|
|
||||||
def add_print(self, last_page=True):
|
def add_print(self, last_page=True):
|
||||||
if last_page:
|
if last_page:
|
||||||
|
|||||||
Reference in New Issue
Block a user