Render Helper Tools#

These Render Helper Tools facilitate the creation of simple sketches.

Currently this is limited to the default and OpenGL renderers (P2D and P3D). Support for other renderers such as SVG will be added in the future.

All of the tools documented on this page work outside of Jupyter Notebooks, but it is here they are the most useful.

from IPython.display import display

import py5

Making a Single Image#

Below is an introductory example showing how to make a single image.

The first parameter, s: py5.Sketch, includes a typehint. The typehint makes tab completion provide the right fields and methods, simplifying development.

def draw_square(s: py5.Sketch):
    s.background(240)
    s.rect_mode(s.CENTER)
    s.fill(255, 0, 0)
    s.rect(s.width / 2, s.height / 2, 50, 50)

Pass the newly created draw_square function to render_frame(). It will render a single frame sketch with a width and height of 200, 200. The result is returned as a PIL Image object.

img = py5.render_frame(draw_square, 200, 200)

print(type(img))
<class 'PIL.Image.Image'>

Here’s what that image looks like:

img
../_images/5868bc14ac87bb41649e5d5dc49dca98d572c2175e1dfa26f45f52a244de0634.png

Next, add some parameters to draw_square to parameterize the output.

def draw_square(s: py5.Sketch, fill_color, square_size):
    s.background(240)
    s.rect_mode(s.CENTER)
    s.fill(*fill_color)
    s.rect(s.width / 2, s.height / 2, square_size, square_size)

Below, use the new draw_square function with render_frame(). Arguments are passed to draw_square via render_frame()’s draw_kwargs parameter.

You can pass positional arguments with draw_args but using keyword arguments is easier and more readable.

img = py5.render_frame(draw_square, 200, 200,
                       draw_kwargs=dict(fill_color=(255, 0, 255), square_size=100))

img
../_images/660368cf9bd9ac8d9f4b929299ac5c58cf854460f9d5513738d531af80503059.png

@render is the decorator equivalent for render_frame(). This achieves cleaner results, particularly when passing function arguments.

When using decorators, don’t forget to type that @ sign at the beginning. If you forget, it won’t work correctly.

@py5.render(200, 200)
def draw_square(s: py5.Sketch, fill_color, square_size):
    s.background(240)
    s.rect_mode(s.CENTER)
    s.fill(*fill_color)
    s.rect(s.width / 2, s.height / 2, square_size, square_size)

Now you have an easy to use function that outputs images using the passed parameter values.

draw_square((255, 255, 0), 75)
../_images/d34a4002bfafafe37913e0b7a8a78da9e1b91157111eab51dc5a8de339755b7a.png

You can make this more interesting with default arguments and the OpenGL renderer.

@py5.render(200, 200, py5.P2D)
def draw_square(s: py5.Sketch, fill_color, square_size=50):
    s.background(240)
    s.rect_mode(s.CENTER)
    s.fill(*fill_color)
    s.rect(s.width / 2, s.height / 2, square_size, square_size)
img1 = draw_square((0, 255, 255))
img2 = draw_square((255, 0, 0), square_size=10)

display(img1, img2)
../_images/d20eaa255694c66050ddf16723f9633d75a065fd02c73654368792c1d52af02f.png ../_images/863dfc64aa1780cd6015358d9d0ffadb37ce9f02771b06d234c913f891a87f11.png

Finally, apply what you’ve learned to make a more interesting example.

@py5.render(400, 100)
def draw_message(s: py5.Sketch, message, color=(255,)):
    s.background(0)
    s.text_size(40)
    s.text_align(s.CENTER, s.CENTER)
    s.fill(*color)
    s.text(message, s.width / 2, s.height / 2)
msg1 = draw_message('py5')
msg2 = draw_message('is')
msg3 = draw_message('awesome!', color=(255, 0, 0))

display(msg1, msg2, msg3)
../_images/2f56e69643f64ca57c104197ce1d0ecb34f988569f9034b91997f4768deea879.png ../_images/3c8e79440c18c40de37009d84c3ae4c59bc135f77eda556158a46ac1a3cd7948.png ../_images/b619e583b44f38b5b2c786e2c0046266de6d1eff6675c1ed3537613fce455b5f.png

Assembling that into an animated GIF is easy with the PIL Image library.

!mkdir -p images/render_helper/

msg1.save('images/render_helper/py5_is_awesome.gif',
          save_all=True, duration=500, loop=0, append_images=[msg2, msg3])

Here is the animated GIF:

py5 is awesome

Making a Series of Images#

The Render Helper Tools can also create a sequence of images for an animation of some kind.

def draw_cubes(s: py5.Sketch, cube_size=50):
    s.background(255)
    s.no_fill()
    s.translate(s.width / 2, s.height / 2, 50)
    s.rotate_y(s.HALF_PI * s.frame_count / 10)
    s.box(cube_size)

Pass the draw_cubes function to render_frame_sequence() to return a list of PIL Image object.

cubes = py5.render_frame_sequence(draw_cubes, 200, 200, py5.P3D, limit=10)

cubes
[<PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>,
 <PIL.Image.Image image mode=RGB size=200x200>]
display(cubes[0], cubes[5])
../_images/d9b251d5ca0d77f5ce815480ae81fc8b798cc337a893610c4bf26f7c7d457034.png ../_images/78715da99461632e9eee71f36d60b7c433340b424bbfa1cf8390acf8889257de.png

As before, a decorator is available. @render_sequence is the decorator equivalent of render_frame_sequence().

@py5.render_sequence(200, 200, py5.P3D, limit=10)
def draw_cubes(s: py5.Sketch, cube_size=50):
    s.no_fill()
    s.background(255)
    s.translate(s.width / 2, s.height / 2, 50)
    s.rotate_y(s.HALF_PI * s.frame_count / 10)
    s.box(cube_size)
cubes = draw_cubes(75)

display(cubes[0], cubes[5])
../_images/8de0867dfe97d9dd54ae0ce63735a573103280058afc632756ca911f5ca91b6f.png ../_images/fb7fd03c7a6ab6e00acdb900c43f61ecf8b964316e9b652a75f40044eaba9d88.png

Normally py5 sketches have a setup() method that is run once at the beginning of the animation. This method might be used to configure fill and stroke settings that should apply for every frame of the animation. You could leave those kinds of commands in the draw_cubes() function, but it might be nice to be able to break it out into a seperate cube_setup() function, like so:

def cube_setup(s: py5.Sketch):
    s.no_fill()
    s.stroke_weight(3)

@py5.render_sequence(200, 200, py5.P3D, limit=10, setup=cube_setup)
def draw_cubes(s: py5.Sketch, cube_size=50):
    s.background(255)
    s.translate(s.width / 2, s.height / 2, 50)
    s.rotate_y(s.HALF_PI * s.frame_count / 10)
    s.box(cube_size)

Now use the draw_cubes function and create an animated GIF.

cubes = draw_cubes(75)

first_cube = cubes[0]
first_cube.save('images/render_helper/rotating_cube.gif',
                save_all=True, duration=100, loop=0, append_images=cubes[1:])

Here is the GIF:

rotating cube

In the previous example the frame_count field was used to drive the animation, rotating the cube by a different angle in each frame. It worked well, but it might not always be easy or convenient to link changing animation state to frame_count. An alternative approach is to use the Python global keyword.

Below is the same example as before, but without depending on frame_count.

rot_y = 0

@py5.render_sequence(200, 200, py5.P3D, limit=10, setup=cube_setup)
def draw_cubes(s: py5.Sketch, cube_size=50):
    global rot_y
    s.background(255)
    s.translate(s.width / 2, s.height / 2, 50)
    # rot_y gets updated with each frame, changing the state
    rot_y += s.HALF_PI / 10
    s.rotate_y(rot_y)
    s.box(cube_size)
cubes = draw_cubes(75)
display(cubes[0], cubes[5])
../_images/e7cd31c47fc839a2a367ca35060866e8cf128b53b53ac808401a088c9b8429aa.png ../_images/7974e84694b977a0c2a3f55d4d4a1c9445de550d151eab3824c4cfe5ec52457e.png

A Caveat#

In both Processing and py5 it is unwise or impossible to run a sketch inside of another sketch.

These Render Helper Tools are creating and running sketches to provide their functionality. Although you might be tempted to do so, do not use them in any way that places them inside of another running sketch.