-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
75 lines (59 loc) · 1.76 KB
/
display.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
from subprocess import Popen, PIPE
from os import remove, execlp, fork
# constants
XRES = 500
YRES = 500
MAX_COLOR = 255
RED = 0
GREEN = 1
BLUE = 2
DEFAULT_COLOR = [0, 0, 0]
def new_screen(width=XRES, height=YRES):
screen = []
for y in range(height):
row = []
screen.append(row)
for x in range(width):
screen[y].append(DEFAULT_COLOR[:])
return screen
def plot(screen, color, x, y):
newy = YRES - 1 - y
if (x >= 0 and x < XRES and newy >= 0 and newy < YRES):
screen[newy][x] = color[:]
def clear_screen(screen):
for y in range(len(screen)):
for x in range(len(screen[y])):
screen[y][x] = DEFAULT_COLOR[:]
def save_ppm(screen, fname):
f = open(fname, 'w')
ppm = 'P3\n' + str(len(screen[0])) + ' ' + \
str(len(screen)) + ' ' + str(MAX_COLOR) + '\n'
for y in range(len(screen)):
row = ''
for x in range(len(screen[y])):
pixel = screen[y][x]
row += str(pixel[RED]) + ' '
row += str(pixel[GREEN]) + ' '
row += str(pixel[BLUE]) + ' '
ppm += row + '\n'
f.write(ppm)
f.close()
def save_extension(screen, fname):
ppm_name = fname[:fname.find('.')] + '.ppm'
save_ppm(screen, ppm_name)
p = Popen(['convert', ppm_name, fname], stdin=PIPE, stdout=PIPE)
p.communicate()
remove(ppm_name)
def display(screen):
ppm_name = 'pic.ppm'
save_ppm(screen, ppm_name)
p = Popen(['display', ppm_name], stdin=PIPE, stdout=PIPE)
p.communicate()
remove(ppm_name)
def make_animation(name):
name_arg = 'anim/' + name + '*'
name = name + '.gif'
print 'Saving animation as ' + name
f = fork()
if f == 0:
execlp('convert', 'convert', '-delay', '3', name_arg, name)