sb.py 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python3
  2. from PIL import Image
  3. from math import sin, pi
  4. def main():
  5. bg = Image.open('../figures/bead_bg.png')
  6. left = Image.open('../figures/bead_left.png')
  7. right = Image.open('../figures/bead_right.png')
  8. size_x, size_y = bg.size
  9. n = 50
  10. def combine(t):
  11. A = 20
  12. w = 2*2*pi/n
  13. offset = int(A*sin(w*t))
  14. left_c = left.copy()
  15. right_c = right.copy()
  16. left_c.paste(left, box=(offset, 0))
  17. right_c.paste(right, box=(-offset, 0))
  18. left_mask = left_c.split()[0].point(lambda i: i != 0 and 255)
  19. right_mask = right_c.split()[0].point(lambda i: i != 0 and 255)
  20. bg_copy = bg.copy()
  21. bg_copy.paste(left_c, mask=left_mask)
  22. bg_copy.paste(right_c, mask=right_mask)
  23. # bg_copy.show()
  24. return bg_copy
  25. frames = [combine(i) for i in range(n)]
  26. frames[0].save('res.gif', save_all=True, append_images=frames[1:], duration=int(1000/30), loop=10)
  27. if __name__ == '__main__':
  28. main()