Introduction¶
Fresnel is a python library that can ray trace publication quality images in real time. It provides a simple python API to define a scene consisting of any number of geometry primitives and render it to an output image.
To start, import the fresnel
python module.
In [1]:
import fresnel
Define a scene¶
A Scene defines a coordinate system, the camera view, the light sources, and contains a number of geometry primitives. Create a new Scene class instance. Scenes come with a default automatic camera that fits the geometry and a default set of lights.
In [2]:
scene = fresnel.Scene()
Add geometry to the scene¶
A Scene may consist of any number of geometry objects. Each geometry object consists of N primitives of the same type, and a material that describes how the primitives interact with light sources. Create 8 spheres with radius 1.0.
In [3]:
geometry = fresnel.geometry.Sphere(scene, N=8, radius=1.0)
Geometry objects have a number of per-primitive attributes. These are exposed with an interface compatible with numpy arrays, and can copy data from numpy arrays efficiently. Set the positions of the spheres:
In [4]:
geometry.position[:] = [[1,1,1],
[1,1,-1],
[1,-1,1],
[1,-1,-1],
[-1,1,1],
[-1,1,-1],
[-1,-1,1],
[-1,-1,-1]]
Set the material of the geometry object to a rough blue surface:
In [5]:
geometry.material = fresnel.material.Material(color=fresnel.color.linear([0.25,0.5,0.9]),
roughness=0.8)
Render the scene¶
preview quickly renders the scene from the view point of the camera.
In [6]:
fresnel.preview(scene)
Out[6]:
Enable anti-aliasing if you plan to use the output of preview for production use. The scene will take longer to render, but will have crisp edges.
In [7]:
fresnel.preview(scene, aa_level=3)
Out[7]:
preview only applies direct lighting. Use pathtrace to account for indirect lighting. (anti-aliasing is always enabled when path tracing).
In [8]:
fresnel.pathtrace(scene)
Out[8]:
The resulting image is noisy. Increase the number of light samples to obtain a clean image.
In [9]:
fresnel.pathtrace(scene, light_samples=40)
Out[9]:
Save output¶
preview and pathtrace return output buffers that can be used like
HxWx4
RGBA numpy arrays. You can pass this standard format on to
other python libraries that work images (e.g.
matplotlib).
In [10]:
out = fresnel.preview(scene, aa_level=3)
print(out[:].shape)
print(out[:].dtype)
(370, 600, 4)
uint8
Use Pillow to save the rendered output to a png file with transparency.
In [11]:
import PIL
In [12]:
image = PIL.Image.fromarray(out[:], mode='RGBA')
image.save('output.png')
To save a JPEG, create an RGB image. This ignores the alpha channel, so the scene background color will show.
In [13]:
image = PIL.Image.fromarray(out[:,:,0:3], mode='RGB')
image.save('output.jpeg')
This is what output.jpeg
looks like (the default background color is
black):
In [16]:
import IPython.display
IPython.display.Image('output.jpeg')
Out[16]:
This page was generated from a jupyter notebook. You can download and run the notebook locally from the fresnel-examples repository.