removed multiprocessing for now
This commit is contained in:
@@ -11,8 +11,6 @@ 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
|
||||||
from brother_ql import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel
|
from brother_ql import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel
|
||||||
|
|
||||||
import multiprocessing
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
stdout = sys.stdout.buffer
|
stdout = sys.stdout.buffer
|
||||||
except:
|
except:
|
||||||
@@ -26,9 +24,6 @@ except:
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
defaultCores = multiprocessing.cpu_count()
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('image', help='The image file to create a label from.')
|
parser.add_argument('image', help='The image file to create a label from.')
|
||||||
parser.add_argument('outfile', nargs='?', type=argparse.FileType('wb'), default=stdout, help='The file to write the instructions to. Defaults to stdout.')
|
parser.add_argument('outfile', nargs='?', type=argparse.FileType('wb'), default=stdout, help='The file to write the instructions to. Defaults to stdout.')
|
||||||
@@ -38,7 +33,6 @@ def main():
|
|||||||
parser.add_argument('--threshold', '-t', type=float, default=70.0, help='The threshold value (in percent) to discriminate between black and white pixels.')
|
parser.add_argument('--threshold', '-t', type=float, default=70.0, help='The threshold value (in percent) to discriminate between black and white pixels.')
|
||||||
parser.add_argument('--no-cut', dest='cut', action='store_false', help="Don't cut the tape after printing the label.")
|
parser.add_argument('--no-cut', dest='cut', action='store_false', help="Don't cut the tape after printing the label.")
|
||||||
parser.add_argument('--loglevel', type=lambda x: getattr(logging, x), default=logging.WARNING, help='Set to DEBUG for verbose debugging output to stderr.')
|
parser.add_argument('--loglevel', type=lambda x: getattr(logging, x), default=logging.WARNING, help='Set to DEBUG for verbose debugging output to stderr.')
|
||||||
parser.add_argument('--cores', '-c', type=int, default=defaultCores, help='The number of cores to use on creating the bin file.')
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
logging.basicConfig(level=args.loglevel)
|
logging.basicConfig(level=args.loglevel)
|
||||||
@@ -58,14 +52,11 @@ def main():
|
|||||||
|
|
||||||
qlr.exception_on_warning = True
|
qlr.exception_on_warning = True
|
||||||
|
|
||||||
coresMessage = "Attempting to create using " + str(args.cores) + " cores."
|
create_label(qlr, args.image, args.label_size, threshold=args.threshold, cut=args.cut, rotate=args.rotate)
|
||||||
logger.debug(coresMessage)
|
|
||||||
|
|
||||||
create_label(qlr, args.image, args.label_size, cores=args.cores, threshold=args.threshold, cut=args.cut, rotate=args.rotate)
|
|
||||||
|
|
||||||
args.outfile.write(qlr.data)
|
args.outfile.write(qlr.data)
|
||||||
|
|
||||||
def create_label(qlr, image, label_size, cores, threshold=70, cut=True, **kwargs):
|
def create_label(qlr, image, label_size, threshold=70, cut=True, **kwargs):
|
||||||
|
|
||||||
label_specs = label_type_specs[label_size]
|
label_specs = label_type_specs[label_size]
|
||||||
dots_printable = label_specs['dots_printable']
|
dots_printable = label_specs['dots_printable']
|
||||||
@@ -155,7 +146,7 @@ def create_label(qlr, image, label_size, cores, threshold=70, cut=True, **kwargs
|
|||||||
qlr.add_compression(True)
|
qlr.add_compression(True)
|
||||||
except BrotherQLUnsupportedCmd:
|
except BrotherQLUnsupportedCmd:
|
||||||
pass
|
pass
|
||||||
qlr.add_raster_data(im, cores)
|
qlr.add_raster_data(im)
|
||||||
qlr.add_print()
|
qlr.add_print()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
+10
-82
@@ -20,10 +20,6 @@ from .devicedependent import models, \
|
|||||||
|
|
||||||
from . import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel, BrotherQLRasterError
|
from . import BrotherQLError, BrotherQLUnsupportedCmd, BrotherQLUnknownModel, BrotherQLRasterError
|
||||||
|
|
||||||
import multiprocessing
|
|
||||||
|
|
||||||
from multiprocessing import Process, Manager
|
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -175,7 +171,7 @@ 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, image, cores=1):
|
def add_raster_data(self, image):
|
||||||
""" image: Pillow Image() """
|
""" image: Pillow Image() """
|
||||||
logger.info("raster_image_size: {0}x{1}".format(*image.size))
|
logger.info("raster_image_size: {0}x{1}".format(*image.size))
|
||||||
image = image.transpose(Image.FLIP_LEFT_RIGHT)
|
image = image.transpose(Image.FLIP_LEFT_RIGHT)
|
||||||
@@ -184,90 +180,22 @@ 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'))
|
||||||
|
|
||||||
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()
|
file_str = BytesIO()
|
||||||
|
while start + row_len <= frame_len:
|
||||||
if cores > 1:
|
row = frame[start:start+row_len]
|
||||||
|
start += row_len
|
||||||
len_prop = float(float(frame_len) / (float(row_len) * float(cores)))
|
file_str.write(b'\x67\x00')
|
||||||
len_check = len_prop.is_integer()
|
if self._compression:
|
||||||
logger.debug("Using " + str(cores) + " cores in add_raster_data.")
|
row = packbits.encode(row)
|
||||||
processes = {}
|
file_str.write(bytes([len(row)]))
|
||||||
manager = Manager()
|
file_str.write(row)
|
||||||
g_data = manager.list(['']*cores)
|
self.data += file_str.getvalue()
|
||||||
|
|
||||||
#Check if we can equally assign subframes to processes, or adjust their length to fit.
|
|
||||||
if len_check:
|
|
||||||
subframe_len = int(frame_len/cores)
|
|
||||||
for i in range(0, cores):
|
|
||||||
|
|
||||||
subframe_start =int(subframe_len * i)
|
|
||||||
subframe_limit = int(subframe_len * (i+1))
|
|
||||||
subframe = frame[subframe_start:subframe_limit]
|
|
||||||
processes[i] = Process(target=self.processFrame, args=(0, row_len, subframe_len, subframe, i, g_data,))
|
|
||||||
processes[i].start()
|
|
||||||
else:
|
|
||||||
num_rows = int(float(frame_len) / (float(row_len) * float(cores)))
|
|
||||||
last_limit = 0
|
|
||||||
subframe_len = int(row_len * num_rows)
|
|
||||||
for i in range(0, cores - 1):
|
|
||||||
|
|
||||||
subframe_start =int(subframe_len * i)
|
|
||||||
subframe_limit = int(subframe_len * (i+1))
|
|
||||||
last_limit = subframe_limit
|
|
||||||
subframe = frame[subframe_start:subframe_limit]
|
|
||||||
processes[i] = Process(target=self.processFrame, args=(0, row_len, subframe_len, subframe, i, g_data,))
|
|
||||||
processes[i].start()
|
|
||||||
|
|
||||||
subframe = frame[last_limit:frame_len]
|
|
||||||
processes[cores - 1] = Process(target=self.processFrame, args=(0, row_len, frame_len - last_limit, subframe, cores - 1, g_data,))
|
|
||||||
processes[cores-1].start()
|
|
||||||
|
|
||||||
for i in range(0, cores):
|
|
||||||
processes[i].join()
|
|
||||||
|
|
||||||
for i in range(0, cores):
|
|
||||||
self.data += g_data[i]
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
logger.debug("Using 1 core in add_raster_data (" + str(cores) + " given).")
|
|
||||||
|
|
||||||
while start + row_len <= frame_len:
|
|
||||||
row = frame[start:start+row_len]
|
|
||||||
start += row_len
|
|
||||||
file_str.write(b'\x67\x00')
|
|
||||||
if self._compression:
|
|
||||||
row = packbits.encode(row)
|
|
||||||
file_str.write(bytes([len(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:
|
||||||
self.data += b'\x1A' # 0x1A = ^Z = SUB; here: EOF = End of File
|
self.data += b'\x1A' # 0x1A = ^Z = SUB; here: EOF = End of File
|
||||||
else:
|
else:
|
||||||
self.data += b'\x0C' # 0x0C = FF = Form Feed
|
self.data += b'\x0C' # 0x0C = FF = Form Feed
|
||||||
|
|
||||||
|
|
||||||
def processFrame(self, start, row_len, frame_len, frame, index, g_data):
|
|
||||||
|
|
||||||
file_str = BytesIO()
|
|
||||||
while start + row_len <= frame_len:
|
|
||||||
row = frame[start:start+row_len]
|
|
||||||
start += row_len
|
|
||||||
#self.data += b'\x67\x00' # g 0x00
|
|
||||||
file_str.write(b'\x67\x00')
|
|
||||||
if self._compression:
|
|
||||||
row = packbits.encode(row)
|
|
||||||
#self.data += bytes([len(row)])
|
|
||||||
#self.data += row
|
|
||||||
file_str.write(bytes([len(row)]))
|
|
||||||
file_str.write(row)
|
|
||||||
|
|
||||||
g_data[index] = file_str.getvalue()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user