-
Notifications
You must be signed in to change notification settings - Fork 4
/
print.py
71 lines (52 loc) · 2.21 KB
/
print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
import asyncio
import logging
import sys
import os
import numpy as np
from pricetag_printer.ble import run_ble
from pricetag_printer.img import read_img
EPD_WIDTH = 250
EPD_HEIGHT = 128
def parse_args():
args = argparse.ArgumentParser(
description='Sends an image to the EPD BLE service running in the ATC_TLSR_Paper Hanshow open source firmware.')
args.add_argument('filename', type=str)
args.add_argument('--log-level', type=str,
choices=['debug', 'info', 'warn', 'error'], default='info')
args.add_argument('--img-binarization-algo', type=str,
choices=['mean-threshold', 'floyd-steinberg'], default='floyd-steinberg',
help='Which image binarization algorithm to use.')
args.add_argument('--show-preview', action='store_true',
help='If set, displays the final image and asks the user for confirmation before printing.')
args.add_argument('--devicename', type=str, default='GT01',
help='Specify the Bluetooth device name to search for. Default value is GT01.')
args.add_argument('--img-invert', action='store_true',
help='Invert resulting pixels.')
return args.parse_args()
def make_logger(log_level):
logger = logging.getLogger('pricetag-printer')
logger.setLevel(log_level)
h = logging.StreamHandler(sys.stdout)
h.setLevel(log_level)
logger.addHandler(h)
return logger
def main():
args = parse_args()
log_level = getattr(logging, args.log_level.upper())
logger = make_logger(log_level)
filename = args.filename
if not os.path.exists(filename):
logger.info('🛑 File not found. Exiting.')
return
bin_img = read_img(args.filename, EPD_HEIGHT,EPD_WIDTH,
logger, args.img_binarization_algo, args.show_preview, args.img_invert)
if bin_img is None:
logger.info(f'🛑 No image generated. Exiting.')
return
logger.info(f'✅ Read image: {bin_img.shape} (h, w) pixels')
data = np.packbits(bin_img)
loop = asyncio.get_event_loop()
loop.run_until_complete(run_ble(data, args.devicename, logger))
if __name__ == '__main__':
main()