Advertisement
6E697465737472796B72

Wink Animation

Feb 24th, 2025
28
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from matplotlib.patches import Circle, Ellipse, Arc
  3. import matplotlib.animation as animation
  4.  
  5. fig, ax = plt.subplots()
  6. head = Circle((0, 0), 1, facecolor='peachpuff', edgecolor='black', lw=2)
  7. right_eye = Circle((0.35, 0.35), 0.15, facecolor='white', edgecolor='black', lw=2)
  8. right_pupil = Circle((0.35, 0.35), 0.07, facecolor='black')
  9. left_eye = Ellipse((-0.35, 0.35), 0.3, 0.3, facecolor='white', edgecolor='black', lw=2)
  10. left_pupil = Circle((-0.35, 0.35), 0.07, facecolor='black')
  11. nose = Ellipse((0, 0.1), 0.2, 0.1, facecolor='sandybrown', edgecolor='black', lw=2)
  12. mouth = Arc((0, -0.2), 0.5, 0.3, angle=0, theta1=20, theta2=160, edgecolor='red', lw=2)
  13. left_eyebrow = Arc((-0.35, 0.55), 0.4, 0.2, angle=0, theta1=0, theta2=180, edgecolor='black', lw=2)
  14. right_eyebrow = Arc((0.35, 0.55), 0.4, 0.2, angle=0, theta1=0, theta2=180, edgecolor='black', lw=2)
  15.  
  16. ax.add_patch(head)
  17. ax.add_patch(right_eye)
  18. ax.add_patch(right_pupil)
  19. ax.add_patch(left_eye)
  20. ax.add_patch(left_pupil)
  21. ax.add_patch(nose)
  22. ax.add_patch(mouth)
  23. ax.add_patch(left_eyebrow)
  24. ax.add_patch(right_eyebrow)
  25.  
  26. ax.set_xlim(-1.2, 1.2)
  27. ax.set_ylim(-1.2, 1.2)
  28. ax.set_aspect('equal')
  29. ax.axis('off')
  30.  
  31. def wink_scale(frame, period=30):
  32.     t = (frame % period) / period
  33.     if t < 0.2:
  34.         return 1.0
  35.     elif t < 0.3:
  36.         return 1.0 - (t - 0.2) / 0.1
  37.     elif t < 0.7:
  38.         return 0.0
  39.     elif t < 0.8:
  40.         return (t - 0.7) / 0.1
  41.     else:
  42.         return 1.0
  43.  
  44. def update(frame):
  45.     scale = wink_scale(frame)
  46.     new_height = 0.3 * scale if scale > 0 else 0.01
  47.     left_eye.set_height(new_height)
  48.     left_pupil.set_visible(scale > 0.5)
  49.     return [left_eye, left_pupil]
  50.  
  51. ani = animation.FuncAnimation(fig, update, frames=200, interval=50, blit=True)
  52. plt.show()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement