python画爱心带粒子特效
时间: 2024-10-01 20:03:24 浏览: 49
在Python中,可以使用PIL库(Python Imaging Library)或者更现代的matplotlib和numpy等结合一些动画效果库如`moviepy`来绘制爱心并添加粒子特效。下面是一个简单的例子,展示如何利用matplotlib和`scipy.ndimage`创建动态爱心,并模拟粒子运动:
```python
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
import numpy as np
# 创建爱心形状
def create_heart(size=50):
x, y = np.mgrid[-size:size+1, -size:size+1]
dist = ((x**2 + (y-1)**2)**0.5).astype(np.float64)
return (dist <= size/2).astype(float)
# 添加粒子特效
def add_particles(image, num_particles=100):
particles = np.random.rand(num_particles, 2) * image.shape[:2] - image.shape[:2]/2
image += np.array([gaussian_filter(particles[:,i], sigma=1) for i in range(2)])
return image
# 创建初始图像并更新
heart = create_heart()
fig, ax = plt.subplots()
ani = FuncAnimation(fig, lambda i: ax.imshow(add_particles(heart), cmap='hot'), frames=range(100), interval=50, blit=True)
plt.show()
阅读全文