This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
forked from savaga/mcm-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
png2mcm.py
118 lines (105 loc) · 3.27 KB
/
png2mcm.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import png
from pprint import pprint, pformat
import pdb
import sys, getopt
import contextlib
@contextlib.contextmanager
def smart_open(filename=None):
if filename and filename != '-':
fh = open(filename, 'w')
else:
fh = sys.stdout
try:
yield fh
finally:
if fh is not sys.stdout:
fh.close()
def print_help(name):
print '%s -i <inputfile.png> -o <outputfile.mcm> [-a|--ascii] [-x|--xsize] <x size> [-y|--ysize] <y size> [-s|--start] <start> [-t|--transparency] [-b|--brightness]' % name
ascii_print = None
transparency = 200
brightness = 20
inputfile = None
outputfile = None
xchars = 0
ychars = 0
startat = 0
try:
opts, args = getopt.getopt(sys.argv[1:],"pahi:o:x:y:s:t:b:",["ifile=","ofile=","ascii","xsize=","ysize=","start","transparency=","brightness="])
except getopt.GetoptError:
print_help(sys.argv[0])
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_help(sys.argv[0])
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-a", "--ascii"):
ascii_print = True
elif opt in ("-x", "--xsize"):
xchars = int(arg)
elif opt in ("-y", "--ysize"):
ychars = int(arg)
elif opt in ("-s", "--start"):
startat = int(arg)
elif opt in ("-t", "--transparency"):
transparency = int(arg)
elif opt in ("-b", "--brightness"):
brightness = int(arg)
if inputfile is None:
print_help(sys.argv[0])
sys.exit(1)
p = png.Reader(filename=inputfile)
t = p.read_flat()
xsize = t[0]
ysize = t[1]
data = t[2]
if xchars > xsize/12:
xchars = xsize/12
else:
xchars = xchars or xsize/12
if ychars > ysize/18:
ychars = ysize/18
else:
ychars = ychars or ysize/18
current_char = 0
with smart_open(outputfile) as fh:
if ascii_print:
for i in xrange(0, len(data), xsize*4):
for j in xrange(0, xsize*4, 4):
if data[i + j + 1] == 0:
fh.write('#')
else:
fh.write(' ')
fh.write('\n')
else:
print >>fh, "MAX7456"
while startat > current_char:
for b in xrange(64):
print >>fh, "01010101"
current_char += 1
for line in xrange(ychars):
for char in xrange(xchars):
for y in xrange(18):
for byte in xrange(3):
out = 0
for bit in xrange(4):
addr = bit*4 + byte*16 + y*xsize*4 + char*12*4 + line*xsize*4*18
d = data[addr:addr+4]
# 2-White,1-transp,0-black
if d[3] < transparency:
out += 1 << ((3-bit)*2)
continue
if max(d[0:3]) - min(d[0:3]) > brightness:
out += 2 << ((3-bit)*2)
print >>fh, format(out, '08b')
for pad in xrange(10):
print >>fh, "11111111"
current_char += 1
while current_char < 256:
for b in xrange(64):
print >>fh, "01010101"
current_char += 1