123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #!/usr/bin/env python3
- from PIL import Image
- from math import sin, pi
- def main():
- bg = Image.open('../figures/bead_bg.png')
- left = Image.open('../figures/bead_left.png')
- right = Image.open('../figures/bead_right.png')
- size_x, size_y = bg.size
- n = 50
- def combine(t):
- A = 20
- w = 2*2*pi/n
- offset = int(A*sin(w*t))
- left_c = left.copy()
- right_c = right.copy()
- left_c.paste(left, box=(offset, 0))
- right_c.paste(right, box=(-offset, 0))
- left_mask = left_c.split()[0].point(lambda i: i != 0 and 255)
- right_mask = right_c.split()[0].point(lambda i: i != 0 and 255)
- bg_copy = bg.copy()
- bg_copy.paste(left_c, mask=left_mask)
- bg_copy.paste(right_c, mask=right_mask)
- # bg_copy.show()
- return bg_copy
- frames = [combine(i) for i in range(n)]
- frames[0].save('res.gif', save_all=True, append_images=frames[1:], duration=int(1000/30), loop=10)
- if __name__ == '__main__':
- main()
|