-
Notifications
You must be signed in to change notification settings - Fork 1
/
inkyconvert.py
70 lines (57 loc) · 1.97 KB
/
inkyconvert.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
#!/usr/bin/env python3
# Modified from https://github.com/RubenLagrouw/inkyconv
from os import remove
from PIL import Image
import subprocess
def convert(imgpath, outpath, example='InkypHAT-212x104.png', dimensions=(122, 122)):
subprocess.run(
'convert {in_file} -auto-orient -geometry {x}x{y}^ -gravity center -crop {x}x{y}+0+0 -strip -type Palette -remap {ex} -dither FloydSteinberg temp.png'
.format(in_file=imgpath, x=dimensions[0], y=dimensions[1], ex=example), shell=True)
img = Image.open("temp.png")
cR = [255, 0, 0]
cB = [0, 0, 0]
cW = [255, 255, 255]
new_pixdata = []
old_pixdata = img.getdata()
palette_old = img.getpalette()
if palette_old == None: # Grayscale Image
for pix in old_pixdata:
if pix == 0:
new_pixdata.append(0)
else:
new_pixdata.append(1)
img = Image.new("P", dimensions)
#img.putdata(newpix)
#img.putpalette(cW + cB + cR)
#img.save('converted.png')
#return -1
else: # Coloured Image
p_conv = {0: palette_old[0:3],
1: palette_old[3:6],
2: palette_old[6:9]}
# p_target = {0: cW,
# 1: cB,
# 2: cR}
for pix in old_pixdata:
if pix not in p_conv:
print('Pixel out of range: {}'.format(pix))
new_pixdata.append(0)
elif p_conv[pix] == cW:
new_pixdata.append(0)
elif p_conv[pix] == cB:
new_pixdata.append(1)
elif p_conv[pix] == cR:
new_pixdata.append(2)
img.putdata(new_pixdata)
img.putpalette(cW + cB + cR) # Goal palette
remove('temp.png')
if not outpath.lower().endswith('.png'):
outpath += '.png'
img.save(outpath)
return 0
if __name__ == '__main__':
import sys
if len(sys.argv) != 3:
print('Improper amount of arguments')
sys.exit(-1)
convert(sys.argv[1], sys.argv[2])