-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.py
34 lines (28 loc) · 981 Bytes
/
renderer.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
"""
renderer.py
Provide an easy interface for pixel-by-pixel rendering.
"""
import itertools as it
import time
from PIL import Image
from PIL.ImageDraw import ImageDraw
from config import MAX_X, MAX_Y
def render(rgb_from_coords=None, filename="mandelbrot.png"):
"""
Display an image of dimensions MAX_X, MAX_Y, delegating to rgb_from_coords
to choose the colour for the current pixel.
:param rgb_from_coords:Function mapping x, y to (r, g, b).
"""
if rgb_from_coords is None:
raise ValueError("Pass in a callback function (x, y)->(r,g,b).")
img = Image.new("RGB", (MAX_X, MAX_Y))
draw = ImageDraw(img)
t1 = time.time()
for coords in it.product(range(MAX_X), range(MAX_Y)):
rgb = rgb_from_coords(*coords)
draw.point(coords, rgb)
t2 = time.time()
print("Calculation and rendering took", t2-t1, "seconds in total.")
img.save(filename)
if __name__ == "__main__":
display(lambda x, y: (x, y, x * y))