Advertisement
Guest User

MoviePy TryOut

a guest
May 6th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4. # #MoviePy Demo
  5.  
  6. # #### Import packages
  7.  
  8. # In[1]:
  9.  
  10. import gizeh
  11. from moviepy import editor as movie
  12. import numpy
  13.  
  14.  
  15. # #### Define parameters
  16.  
  17. # In[ ]:
  18.  
  19. width, height = 256, 256   # width, height, in pixels
  20. cx, cy = width/2, height/2 # center in pixels
  21. rmin, rmax = 50, 100       # minimum and maximum radius
  22. Pi = 3.14                  # defining pi
  23. tmax = 2.0                 # duration of the clip, in seconds
  24. freq = numpy.pi * 2 / tmax # pulsation frequency
  25.  
  26.  
  27. # #### Function to draw a single frame
  28.  
  29. # In[ ]:
  30.  
  31. def make_frame(t):
  32.    
  33.     # width, height
  34.     surface = gizeh.Surface(width, height, bg_color=(0, 0, .3))
  35.    
  36.     # the diameter varies over time
  37.     d = rmin + (rmax - rmin) * (1 + numpy.sin(t * freq)) / 2
  38.    
  39.     # the radius varies over time
  40.     r = rmin + (rmax - rmin) * (1 + numpy.sin(t * freq/2)) / 2
  41.    
  42.    
  43.     # let's draw something
  44.    
  45.     circle = gizeh.circle(d/4, xy = (cx, cy), fill=(1, 1, 1))
  46.     circle.draw(surface)
  47.     circle = gizeh.circle(d/5, xy = (cx, cy), fill=(1, 0, 0))
  48.     circle.draw(surface)
  49.     arc = gizeh.arc(r, a1=r*Pi*0.5, a2=r*Pi*0., xy = (cx, cy), stroke=(1,1,1), stroke_width= 15)
  50.     arc.draw(surface)
  51.    
  52.     # returns a 8-bit RGB array
  53.     return surface.get_npimage()
  54.  
  55.  
  56. # #### Create the clip
  57.  
  58. # In[ ]:
  59.  
  60. clip = movie.VideoClip(make_frame, duration=tmax)
  61.  
  62.  
  63. # #### Preview in the browser
  64.  
  65. # In[ ]:
  66.  
  67. clip.ipython_display(fps=20, autoplay=1, loop=4)
  68.  
  69.  
  70. # #### Create an animated GIF
  71.  
  72. # In[ ]:
  73.  
  74. rm -f "./output/animation.gif"
  75.  
  76.  
  77. # In[ ]:
  78.  
  79. mkdir "output"
  80.  
  81.  
  82. # In[ ]:
  83.  
  84. #output to the new one
  85. clip.write_gif("./output/animation.gif", fps=20)
  86.  
  87.  
  88. # [Download Image](./output/animation.gif)
  89.  
  90. # In[ ]:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement