cookies.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. from PIL import Image
  3. from math import sin, cos, pi
  4. def main():
  5. bg = Image.open('./stars.png')
  6. cookie = Image.open('./cookie.png').resize((100, 100))
  7. cookie_mask = cookie.split()[0].point(lambda i: i != 0 and 255)
  8. muffin = Image.open('./muffin.png').resize((100, 100))
  9. muffin_mask = muffin.split()[0].point(lambda i: i != 0 and 255)
  10. doughnut = Image.open('./doughnut.png').resize((100, 100))
  11. doughnut_mask = doughnut.split()[0].point(lambda i: i != 0 and 255)
  12. snacks = [cookie, muffin, doughnut]
  13. masks = [cookie_mask, muffin_mask, doughnut_mask]
  14. n = 150
  15. def combine(t):
  16. A = 0.1
  17. w = 6*2*pi/n
  18. bg_copy = bg.copy()
  19. # left_c.paste(left, box=(offset, 0))
  20. # right_c.paste(right, box=(-offset, 0))
  21. N = 10
  22. radius = 400
  23. center = 450, 450
  24. for i in range(N+1):
  25. strain_x = 1 + A*sin(w*t)
  26. strain_y = 1 - A*sin(w*t)
  27. theta = i*2*pi/(N+1)
  28. x = int(center[0] + strain_x*radius*cos(theta))
  29. y = int(center[1] + strain_y*radius*sin(theta))
  30. bg_copy.paste(snacks[i % 3], mask=masks[i % 3], box=(x, y))
  31. # bg.show()
  32. return bg_copy
  33. # combine(0)
  34. frames = [combine(i) for i in range(n)]
  35. frames[0].save('snacks.gif', save_all=True, append_images=frames[1:], duration=int(1000/30), loop=0)
  36. if __name__ == '__main__':
  37. main()