-
Notifications
You must be signed in to change notification settings - Fork 10
/
text-to-image.py
82 lines (60 loc) · 1.96 KB
/
text-to-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
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
import sys
from PIL import Image
def readFile(filepath):
with open(filepath) as f:
content = f.read()
return content
for path in sys.argv[1:]:
filename = path.split('/')[-1].split('.')[0]
text = readFile(path)
lines = text.split('\n')
imageWidth = 0
imageHeight = 0
for line in lines:
split = line.split(' ')
marker = split[0]
if len(marker) == 3:
index = int(marker[0:2])
width = len(split) - 1
height = index + 1
if width > imageWidth:
imageWidth = width;
if height > imageHeight:
imageHeight = height
pixels = []
for y in range(imageHeight):
for x in range(imageWidth):
pixels.append((0, 0, 0, 0))
for line in lines:
split = line.split(' ')
marker = split[0]
if len(marker) == 3:
index = int(marker[0:2])
for x in range(len(split) - 1):
s = split[x + 1]
if s != '~':
r = 0
g = 0
b = 0
if s == 'a':
r = g = b = 107
elif s == 'b':
r = g = b = 187
else:
c = ord(s[0]) - 33
b = (c & 3) * 64
if b == 192:
b += 63
c = c >> 2
g = (c & 3) * 64
if g == 192:
g += 63
c = c >> 2
r = (c & 3) * 64
if r == 192:
r += 63
i = (index * imageWidth) + x
pixels[i] = (r, g, b, 255)
image = Image.new('RGBA', (imageWidth, imageHeight))
image.putdata(pixels)
image.save('output/%s.png' % filename)