-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg2PyEpd
85 lines (69 loc) · 2.64 KB
/
img2PyEpd
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# PyEPD
# Gordon Inggs ([email protected])
# November 2017
from pyepd import ImageHandler, DisplayPanelController
import tempfile
import errno
import argparse
import os.path
# Modified from https://stackoverflow.com/a/25868839
def is_writable(path):
try:
dir_path = os.path.dirname(path)
with tempfile.TemporaryFile(dir=dir_path) as testfile:
pass
except OSError as e:
if e.errno == errno.EACCES: # 13
return False
return True
CONTROLLERS = {'TCM-P74-230': DisplayPanelController.TC_P74_230}
DEFAULT_CONTROLLER = 'TCM-P74-230'
# Argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("input_image", type=str,
help="Path of input image file")
parser.add_argument("output_image", type=str,
help="Path of output image file")
parser.add_argument("-w", "--white-background", action="store_true",
help="Set background to be white. Default is median of image colours.")
parser.add_argument("-b", "--black-background", action="store_true",
help="Set background to be black. Default is median of image colours.")
parser.add_argument("-r", "--rotate", action="count",
help="Rotate image 90° right. May be used multiple times.")
parser.add_argument("-d", "--device", type=str, choices=CONTROLLERS.keys(),
help="Select display panel controller. Default is {}.".format(DEFAULT_CONTROLLER))
args = parser.parse_args()
# Argument validation and wrangling
# Input image
if not os.path.exists(args.input_image):
raise IOError("{} doesn't exist!".format(args.input_image))
# Output location and access
if not os.path.exists(os.path.dirname(args.output_image)):
raise IOError("{} doesn't exist".format(
os.path.dirname(args.output_image)))
elif not is_writable(args.output_image):
raise IOError("Can't write to {}".format(args.output_image))
# Setting up EPD controller
if args.device is None:
args.device = DEFAULT_CONTROLLER
device = CONTROLLERS[args.device]()
# Setting background flag if present
if args.white_background:
background = 255
elif args.black_background:
background = 0
else:
background = -1
if args.rotate is not None:
rotate_count = args.rotate
else:
rotate_count = 0
# Acquiring the image
with ImageHandler.acquire_and_normalise(args.input_image, device, background, rotate_count) as image:
# Converting it
image_data = ImageHandler.convert(image)
# Generating the EPD
output_image_data = device.assemble_epd(image_data)
# Writing out the file
output_image_data.tofile(args.output_image)