【发布时间】:2020-10-23 06:26:07
【问题描述】:
我正在尝试使用 Python 对参数复杂函数的颜色映射表示进行动画处理。 我逐渐将一些东西放在一起,并检查它们是否正常工作。但我无法保存动画。
我遇到了这个错误:
Requested MovieWriter (ffmpeg) not available
但是,我的系统上确实安装了ffmpeg,
在 Windows 控制台上ffmpeg -version 返回有关 ffmpeg 的各种信息。另外,我还使用 pip pip install ffmpeg 在 Python 脚本目录中安装了 ffmpeg,这是成功的。我还在我的代码中设置了 ffmepg 路径:plt.rcParams['animation.ffmpeg_path'] = "C:\FFmpeg\bin\ffmpeg.exe"
我的想法已经用完了。
这是我的代码。 感谢您的阅读。
import numpy as np
import math
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
pi=math.pi
plt.rcParams['animation.ffmpeg_path'] = "C:\FFmpeg\bin\ffmpeg.exe"
fig = plt.figure()
def complex_array_to_rgb(X, theme='dark', rmax=None):
absmax = rmax or np.abs(X).max()
Y = np.zeros(X.shape + (3,), dtype='float')
Y[..., 0] = np.angle(X) / (2 * pi) % 1
if theme == 'light':
Y[..., 1] = np.clip(np.abs(X) / absmax, 0, 1)
Y[..., 2] = 1
elif theme == 'dark':
Y[..., 1] = 1
Y[..., 2] = np.clip(np.abs(X) / absmax, 0, 1)
Y = matplotlib.colors.hsv_to_rgb(Y)
return Y
# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
fps = 10
nSeconds = 1
snapshots = [ complex_array_to_rgb(np.array([[3*(x + 1j*y)**(2.9+p/300) + 1/(x + 1j*y)**2 for x in np.arange(-1,1,0.05)] for y in np.arange(-1,1,0.05)])) for p in range( nSeconds * fps ) ]
fig = plt.figure( figsize=(3,3) )
Z2=snapshots[0]
im=plt.imshow(Z2, extent=(-1,1,-1,1))
def animate_func(i):
if i % fps == 0:
print( '.')
im.set_array(snapshots[i])
return [im]
anim = animation.FuncAnimation(
fig,
animate_func,
frames = nSeconds * fps,
interval = 1000 / fps, # in ms
)
anim.save('test_anim.mp4', writer=writer)
【问题讨论】:
标签: python matplotlib animation ffmpeg