Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building an animation using Python Gizeh

I am able to create a simple diagram with shapes and numbers. I am using the following code:

import gizeh as gz

W, H = 500, 300

surface = gz.Surface(W,H, bg_color=(1,0.7,1))

for a in range(1,9):
    rect = gz.rectangle(lx = 10, ly = 10, xy=(W/a,H/a), fill =(0,1,0.7))
    rect.draw(surface)
    txt = gz.text(str(a), fontfamily="Dancing Script", fontsize=15, fill=(0,0,0),xy=(W/a,H/a))
    txt.draw(surface)

surface.ipython_display()

I have also created a version using moviepy:

import numpy as np
import gizeh as gz
import moviepy.editor as mpy

W, H = 500, 300
duration = 5
figpath = '/tmp/'
fps = 1

def make_frame(t):

    surface = gz.Surface(W,H, bg_color=(1,1,1))

    rect = gz.rectangle(lx = 10, ly = 10, xy=(W/(t+1),H/2), fill =(0,1,0.7))
    rect.draw(surface)
    txt = gz.text(str(t+1), fontfamily="Dancing Script", fontsize=15, fill=(0,0,0),xy=(W/(t+1),H/2))
    txt.draw(surface)

    return surface.get_npimage()

clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_videofile(figpath + 'trax_0.mp4', fps=fps)

clip.ipython_display(fps=fps, width=W, autoplay=0, loop=0)

I would like to be able to create animated GIF using a time delay between each step of the cycle.

like image 378
Hugo Avatar asked May 31 '15 07:05

Hugo


2 Answers

Try to use MoviePy - a module from the author of Gizeh.
Look at a good article where Gizeh and MoviePy are used for the animation: http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/

like image 130
Delimitry Avatar answered Oct 08 '22 10:10

Delimitry


#!/usr/bin/env python3

import numpy as np
import gizeh as gz
import moviepy.editor as mpy

W,H = 128,128 # 128x128 pixel
duration = 2  # 2 seconds
ncircles = 20 # Number of circles

def make_frame(t):

    surface = gz.Surface(W,H)

    for i in range(ncircles):
        angle = 2*np.pi*(1.0*i/ncircles+t/duration)
        center = W*( 0.5+ gz.polar2cart(0.1,angle))
        circle = gz.circle(r= W*(1.0-1.0*i/ncircles),
                              xy= center, fill= (i%2,i%2,i%2))
        circle.draw(surface)

    return surface.get_npimage()

clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif("circles.gif",fps=15, opt="OptimizePlus", fuzz=10)

enter image description here

like image 44
noobninja Avatar answered Oct 08 '22 09:10

noobninja