-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset_image.py
executable file
·56 lines (38 loc) · 1.35 KB
/
set_image.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
#!/usr/bin/env python3
from argparse import ArgumentParser
import inquirer
from PIL import Image
import zmkx
CANVAS_WIDTH = 128
CANVAS_HEIGHT = 296
def get_device(features=[]):
devices = zmkx.find_devices(features=features)
if len(devices) == 0:
print('未找到符合条件的设备')
return None
if len(devices) == 1:
return devices[0]
choice = inquirer.prompt([
inquirer.List('device', message='有多个设备,请选择', choices=[
(f'{d.manufacturer} {d.product} (SN: {d.serial})', d)
for d in devices
]),
])
if choice is None:
return None
return choice['device']
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('file', help='指定要显示的图片文件')
args = parser.parse_args()
device = get_device(features=['eink'])
if device is None:
exit(1)
with device.open() as device, Image.open(args.file) as image:
image.thumbnail((CANVAS_WIDTH, CANVAS_HEIGHT))
center = ((CANVAS_WIDTH - image.width) // 2,
(CANVAS_HEIGHT - image.height) // 2)
canvas = Image.new('L', (CANVAS_WIDTH, CANVAS_HEIGHT), color=0xFF)
canvas.paste(image, center)
canvas = canvas.convert('1', dither=Image.FLOYDSTEINBERG)
device.eink_set_image(canvas.tobytes())