-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.py
More file actions
104 lines (87 loc) · 2.68 KB
/
Copy pathdisplay.py
File metadata and controls
104 lines (87 loc) · 2.68 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
from subprocess import Popen, PIPE
from os import remove
from PIL import Image
import imageio
import glob
#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 new_zbuffer( width = XRES, height = YRES ):
zb = []
for y in range( height ):
row = [ float('-inf') for x in range(width) ]
zb.append( row )
return zb
def plot( screen, zbuffer, color, x, y, z ):
newy = YRES - 1 - y
z = int((z * 1000)) / 1000.0
if ( x >= 0 and x < XRES and newy >= 0 and newy < YRES and zbuffer[newy][x] <= z):
screen[newy][x] = color[:]
zbuffer[newy][x] = z
def clear_screen( screen ):
for y in range( len(screen) ):
for x in range( len(screen[y]) ):
screen[y][x] = DEFAULT_COLOR[:]
def clear_zbuffer( zb ):
for y in range( len(zb) ):
for x in range( len(zb[y]) ):
zb[y][x] = float('-inf')
def save_ppm( screen, fname ):
f = open( fname, 'wb' )
ppm = 'P6\n' + str(len(screen[0])) +' '+ str(len(screen)) +' '+ str(MAX_COLOR) +'\n'
f.write(ppm.encode())
for y in range( len(screen) ):
for x in range( len(screen[y]) ):
pixel = screen[y][x]
f.write( bytes(pixel) )
f.close()
def save_ppm_ascii( 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 ):
img = Image.new('RGB', (len(screen[0]), len(screen)))
pixels = []
for row in screen:
for pixel in row:
pixels.append( tuple(pixel) )
img.putdata(pixels)
img.save(fname, 'PNG')
def display( screen ):
img = Image.new('RGB', (len(screen[0]), len(screen)))
pixels = []
for row in screen:
for pixel in row:
pixels.append( tuple(pixel) )
img.putdata(pixels)
img.show()
def make_animation( name ):
filenames = glob.glob("anim/" + name + "*");
filenames = sorted(filenames)
print(filenames)
images = []
for filename in filenames:
images.append(imageio.imread(filename))
imageio.mimsave(name+".gif", images, fps=30)