Fork me on BitBucket

From Ray Tracer to Gif Animation

So one of my first python projects when I was younger was to create a raytracer. My raytracer creates a .png image pixel by pixel. To decide what colour to colour a pixel, It sends rays out (in my mathematical model, think of it as a x,y,z grid with equations for a torus, sphere etc). Using basic vector calculus and linear algebra, we bounce the ray around in the grid until it hits the light source (if it doesn't hit the light source after a few reflections we make the pixel a shadow point). Each reflection the ray gains some colour from the object it hits.

To make an animation I generated several .png images (moving the light source a few cm each time). These images were then put into a gif animation with the following code:

__author__ = 'Robert'
from images2gif import writeGif
from PIL import Image
import os

file_names = sorted(
    (fn for fn in os.listdir('.') if fn.endswith('.png'))
#['animation_a.png', 'animation_b.png', ...] "

images = [ for fn in file_names]

size = (150,150)
for im in images:
    im.thumbnail(size, Image.ANTIALIAS)

print writeGif.__doc__
# writeGif(filename, images, duration=0.1, loops=0, dither=1)
#    Write an animated gif from the specified images.
#    images should be a list of numpy arrays of PIL images.
#    ...
#    ...

filename = "my_gif.GIF"
writeGif(filename, images, duration=0.2)
#54 frames written
#Process finished with exit code 0

Output, a 150x150 8bit GIF:

unfortunately to keep the file size down I had to reduce the quality.

page divider

Random Quote