-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (104 loc) · 4.18 KB
/
main.py
File metadata and controls
132 lines (104 loc) · 4.18 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
import argparse
import random
from os import listdir
from os.path import isfile, join
from PIL import Image, ImageDraw
class Picture:
def __init__(self, step_count, height, width):
self.step_count = step_count
self.height = height
self.width = width
self.image = Image.new('RGB', (self.width, self.height), (0, 0, 0))
# draw initial grid
self.draw = ImageDraw.Draw(self.image)
y_start = 0
y_end = self.image.height
step_size = int(self.width / self.step_count)
for x in range(0, self.width, step_size):
line = ((x, y_start), (x, y_end))
self.draw.line(line, fill=(0, 0, 0))
x_start = 0
x_end = self.width
for y in range(0, self.height, step_size):
line = ((x_start, y), (x_end, y))
self.draw.line(line, fill=(0, 0, 0))
def show(self):
self.image.show()
def fill_tile(self, tile_x, tile_y, color):
step_size = int(self.width / self.step_count)
# draw top left to bottom right
x_start = tile_x * step_size + 1
x_end = (tile_x + 1) * step_size - 1
y_start = tile_y * step_size + 1
y_end = (tile_y + 1) * step_size - 1
line = ((x_start, y_start), (x_end, y_end))
self.draw.line(line, fill=color, width=3)
# draw top right to bottom left
x_start = (tile_x + 1) * step_size - 1
x_end = tile_x * step_size + 1
y_start = tile_y * step_size + 1
y_end = (tile_y + 1) * step_size - 1
line = ((x_start, y_start), (x_end, y_end))
self.draw.line(line, fill=color, width=3)
def draw_xsp(self, top_x, top_y, filename, color):
"""Assumes the letter pattern lines are fixed width."""
data = open(filename, 'r').read()
max_x = 0
y_offset = 0
for line in data.split('\n'):
x_offset = 0
for char in line:
if char != ' ':
self.fill_tile(top_x + x_offset, top_y + y_offset, color)
x_offset += 1
if x_offset > max_x:
max_x = x_offset
y_offset += 1
return max_x, y_offset
def draw_letter(self, top_x, top_y, letter, color):
return self.draw_xsp(top_x, top_y, 'patterns/{letter}.xsp'.format(letter=letter), color)
def draw_phrase(self, top_x, top_y, phrase, color):
x = top_x
y = top_y
for letter in phrase:
if letter == ' ':
x += 4
continue
x_offset, _ = self.draw_letter(x, y, letter, color)
x += x_offset
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("width", help="width of image in pixels",
type=int)
parser.add_argument("height", help="height of image in pixels",
type=int)
parser.add_argument("step_count", help="how many steps across the grid",
type=int)
parser.add_argument("message", help="the message to display",
type=str)
parser.add_argument("--input_filename", help="input image filename to use (optional)",
type=str, default=None, required=False)
parser.add_argument("-s", help="open and display the image if set",
action="store_true")
args = parser.parse_args()
_step_count = args.step_count
_height = args.height
_width = args.width
_message = args.message
_filename = args.input_filename
_show = args.s
if _filename is None: # pick an image at random, filter out letters and non-xsp files
files = ['patterns/' + f for f in listdir('patterns') if isfile(join('patterns', f)) and
len(f) > 5]
files = [f for f in files if f.endswith('xsp')]
_filename = random.choice(files)
p = Picture(_step_count, _height, _width)
for _x in range(_width):
for _y in range(_height):
p.fill_tile(_x, _y, (255, 255, 255))
p.draw_phrase(10, 10, _message, (255, 0, 0))
p.draw_xsp(110, 20, _filename, (0, 0, 255))
if _show:
p.show()
p.image.save('output.png')