Skip to content

Instantly share code, notes, and snippets.

@Klafyvel
Created October 20, 2016 13:39
Show Gist options
  • Save Klafyvel/7c7b754d97e7b9dc24358384cd932039 to your computer and use it in GitHub Desktop.
Save Klafyvel/7c7b754d97e7b9dc24358384cd932039 to your computer and use it in GitHub Desktop.
import gizeh
import moviepy.editor as mpy
WIDTH, HEIGHT = 300, 200 # Pixels
DURATION = 1 # Seconds
FPS = 30
NB_CIRCLES = 6
NB_FRAMES = FPS * DURATION
RADIUS = WIDTH / NB_CIRCLES / 2
CIRCLE_Y = 1/3 * HEIGHT
FONT_SIZE = 2*RADIUS/3
TEXT = "-1 C"
LEFT_TO_RIGHT = False
TEXT_CURRENT = "+1 A"
def make_frame(t):
surface = gizeh.Surface(WIDTH, HEIGHT, bg_color=(1, 1, 1))
offset = t / DURATION * 2 * RADIUS
rect = gizeh.rectangle(lx=WIDTH,
ly=5/2*RADIUS,
xy=(WIDTH/2, CIRCLE_Y),
fill=(25/255, 81/255, 107/255))
rect.draw(surface)
rect_counter = gizeh.rectangle(lx=3,
ly=3*RADIUS,
xy=(WIDTH/2, CIRCLE_Y),
fill=(248/255, 173/255, 50/255))
arrow = gizeh.polyline(points=[(1/4*WIDTH, 3/4 * HEIGHT-3/4*FONT_SIZE),
(2/3*WIDTH, 3/4 * HEIGHT-3/4*FONT_SIZE),
(2/3*WIDTH, 3/4 * HEIGHT-3/2*FONT_SIZE),
(3/4*WIDTH, 3/4 * HEIGHT),
(2/3*WIDTH, 3/4 * HEIGHT+3/2*FONT_SIZE),
(2/3*WIDTH, 3/4 * HEIGHT+3/4*FONT_SIZE),
(1/4*WIDTH, 3/4 * HEIGHT+3/4*FONT_SIZE),
(1/4*WIDTH, 3/4 * HEIGHT-3/4*FONT_SIZE)
],
stroke_width=2,
stroke=(248/255, 173/255, 50/255),
fill=(25/255, 81/255, 107/255))
text_direction = gizeh.text("Sens positif",
fontfamily="Helvetica",
fontsize=FONT_SIZE,
xy=(WIDTH/2, 3/4*HEIGHT),
fill=(1, 1, 1)
)
text_current = gizeh.text("I = "+TEXT_CURRENT,
fontfamily="Helvetica",
fontsize=FONT_SIZE,
xy=(WIDTH/2, CIRCLE_Y-3/2*RADIUS-1/2*FONT_SIZE))
text_current.draw(surface)
side_rect_l = gizeh.rectangle(lx=1/5*WIDTH,
ly=HEIGHT,
xy=(0, HEIGHT/2),
fill=gizeh.ColorGradient(
type="linear",
stops_colors=[
(0, (1, 1, 1, 1)),
(0.5, (1, 1, 1, 0)),
],
xy1=(0, HEIGHT/2),
xy2=(1/5*WIDTH, HEIGHT/2)))
side_rect_r = gizeh.rectangle(lx=1/5*WIDTH,
ly=HEIGHT,
xy=(9/10*WIDTH, HEIGHT/2),
fill=gizeh.ColorGradient(
type="linear",
stops_colors=[
(0, (1, 1, 1, 0)),
(0.5, (1, 1, 1, 1)),
(0.75, (1, 1, 1, 1)),
],
xy1=(0, HEIGHT/2),
xy2=(1/5*WIDTH, HEIGHT/2)))
for i in range(NB_CIRCLES+2):
if LEFT_TO_RIGHT:
d = (2*i-1)*RADIUS+offset
else:
d = (2*i-1)*RADIUS-offset
text = gizeh.text(TEXT,
fontfamily="Helvetica",
fontsize=FONT_SIZE,
xy=(d, CIRCLE_Y),
fill=(1, 1, 1))
circle1 = gizeh.circle(RADIUS-2,
xy=(d, CIRCLE_Y),
fill=(8/255, 69/255, 97/255))
circle2 = gizeh.circle(RADIUS,
xy=(d, CIRCLE_Y),
fill=(248/255, 173/255, 50/255))
circle2.draw(surface)
circle1.draw(surface)
text.draw(surface)
rect_counter.draw(surface)
arrow.draw(surface)
text_direction.draw(surface)
side_rect_l.draw(surface)
side_rect_r.draw(surface)
return surface.get_npimage()
clip = mpy.VideoClip(make_frame, duration=DURATION)
clip.write_gif("anim"+TEXT+" "+TEXT_CURRENT+".gif",
fps=FPS, opt="OptimizePlus", fuzz=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment