| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import sys, argparse, logging |
| 4 |
|
| 5 |
from brother_ql.raster import BrotherQLRaster |
| 6 |
from brother_ql.conversion import convert |
| 7 |
from brother_ql.devicedependent import label_type_specs |
| 8 |
|
| 9 |
try: |
| 10 |
stdout = sys.stdout.buffer |
| 11 |
except: |
| 12 |
stdout = sys.stdout |
| 13 |
|
| 14 |
logger = logging.getLogger(__name__) |
| 15 |
|
| 16 |
def main(): |
| 17 |
parser = argparse.ArgumentParser() |
| 18 |
parser.add_argument('image', help='The image file to create a label from.') |
| 19 |
parser.add_argument('outfile', nargs='?', type=argparse.FileType('wb'), default=stdout, help='The file to write the instructions to. Defaults to stdout.') |
| 20 |
parser.add_argument('--model', '-m', default='QL-500', help='The printer model to use. Check available ones with `brother_ql_info list-models`.') |
| 21 |
parser.add_argument('--label-size', '-s', default='62', help='The label size (and kind) to use. Check available ones with `brother_ql_info list-label-sizes`.') |
| 22 |
parser.add_argument('--rotate', '-r', choices=('0', '90', '180', '270'), default='auto', help='Rotate the image (counterclock-wise) by this amount of degrees.') |
| 23 |
parser.add_argument('--threshold', '-t', type=float, default=70.0, help='The threshold value (in percent) to discriminate between black and white pixels.') |
| 24 |
parser.add_argument('--dither', '-d', action='store_true', help='Enable dithering when converting the image to b/w. If set, --threshold is meaningless.') |
| 25 |
parser.add_argument('--compress', '-c', action='store_true', help='Enable compression (if available with the model). Takes more time but results in smaller file size.') |
| 26 |
parser.add_argument('--red', action='store_true', help='Create a label to be printed on black/red/white tape (only with QL-8xx series on DK-22251 labels). You must use this option when printing on black/red tape, even when not printing red.') |
| 27 |
parser.add_argument('--600dpi', action='store_true', dest='dpi_600', help='Print with 600x300 dpi available on some models. Provide your image as 600x600 dpi; perpendicular to the feeding the image will be resized to 300dpi.') |
| 28 |
parser.add_argument('--lq', action='store_false', dest='hq', help='Print with low quality (faster). Default is high quality.') |
| 29 |
parser.add_argument('--no-cut', dest='cut', action='store_false', help="Don't cut the tape after printing the label.") |
| 30 |
parser.add_argument('--loglevel', type=lambda x: getattr(logging, x), default=logging.WARNING, help='Set to DEBUG for verbose debugging output to stderr.') |
| 31 |
args = parser.parse_args() |
| 32 |
|
| 33 |
logging.basicConfig(level=args.loglevel) |
| 34 |
|
| 35 |
args.model = args.model.upper() |
| 36 |
|
| 37 |
try: |
| 38 |
qlr = BrotherQLRaster(args.model) |
| 39 |
except BrotherQLUnknownModel: |
| 40 |
sys.exit("Unknown model. Use the command brother_ql_info list-models to show available models.") |
| 41 |
|
| 42 |
try: |
| 43 |
label_type_specs[args.label_size] |
| 44 |
except ValueError: |
| 45 |
sys.exit("Unknown label_size. Check available sizes with the command brother_ql_info list-label-sizes") |
| 46 |
|
| 47 |
qlr.exception_on_warning = True |
| 48 |
|
| 49 |
create_label(qlr, args.image, args.label_size, threshold=args.threshold, cut=args.cut, rotate=args.rotate, dither=args.dither, compress=args.compress, red=args.red, dpi_600=args.dpi_600, hq=args.hq) |
| 50 |
|
| 51 |
args.outfile.write(qlr.data) |
| 52 |
|
| 53 |
def create_label(qlr, image, label_size, threshold=70, cut=True, dither=False, compress=False, red=False, **kwargs): |
| 54 |
convert(qlr, [image], label_size, threshold=threshold, cut=cut, dither=dither, compress=compress, red=red, **kwargs) |
| 55 |
|
| 56 |
if __name__ == "__main__": |
| 57 |
main() |