-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
61 lines (51 loc) · 1.55 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
from subprocess import Popen, PIPE
from os import remove
#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 ):
x = int(x)
y = int(y)
newy = YRES - 1 - y
if ( x >= 0 and x < XRES and newy >= 0 and newy < YRES ):
screen[x][newy] = color[:]
def clear_screen( screen ):
for y in range( len(screen) ):
for x in range( len(screen[y]) ):
screen[x][y] = 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[x][y]
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 )
Popen( ['display', ppm_name], stdin=PIPE, stdout = PIPE )