farry

barnsley-fern-makevideo.py

Feb 6th, 2021 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import random
  2. from subprocess import Popen, PIPE
  3.  
  4. frames, width, height = 120, 720, 720
  5. ppm_header = bytes("P6\n{} {}\n255\n".format(width, height), "ascii")
  6.  
  7. ff = ['ffmpeg', '-y', '-f', 'image2pipe', '-vcodec', 'ppm', '-r', '24',
  8.       '-i', '-', '-pix_fmt', 'yuv420p', 'barnsley-vid.mp4']
  9. pipe = Popen(ff, stdin=PIPE)
  10.  
  11. for frame in range(frames):
  12.  
  13.     pixels = [0] * (width * height)
  14.     x, y = 0, 1
  15.     grow = (1 - (frame/frames - 1) ** 4) * 0.85
  16.    
  17.     for n in range(60 * width * height):
  18.        
  19.         r = random.random() * 100
  20.         xn, yn = x, y
  21.         if r < 1:
  22.             x = 0
  23.             y = 0.16 * yn
  24.         elif r < 86:
  25.             x = grow * xn + 0.04 * yn
  26.             y = -0.04 * xn + 0.85 * yn + 1.6
  27.         elif r < 93:
  28.             x = 0.20 * xn - 0.26 * yn
  29.             y = 0.23 * xn + 0.22 * yn + 1.6
  30.         else:
  31.             x = -0.15 * xn + 0.28 * yn
  32.             y = 0.26 * xn + 0.24 * yn + 0.44
  33.  
  34.         x_pix = int(width * (0.45 + 0.195 * x))
  35.         y_pix = int(height * (1 - 0.099 * y ))
  36.         pixels[x_pix + y_pix * width] += 1
  37.  
  38.     greys = [max(0, (256 - p) / 256) for p in pixels]
  39.     colors = [int(c * 255) for g in greys for c in [g ** 6, g, g ** 6]]
  40.     pipe.stdin.write(ppm_header + bytes(colors))
  41.  
  42. pipe.stdin.close()
  43. pipe.wait()
  44.  
Add Comment
Please, Sign In to add comment