[英]how to produce a movie with python
I have some binary files with names ('Den_pro_resample" +'_sdf _'+ str(n)+'.dat') where n changes from 0 to 25. 我有一些名称为('Den_pro_resample“ +'_ sdf _'+ str(n)+'。dat'的二进制文件,其中n从0变为25。
I wrote a code to read these files and then plot the results with imshow command. 我编写了代码来读取这些文件,然后使用imshow命令绘制结果。
at the last step, I want to create a movie . 在最后一步,我想制作一部电影。 to do that, first, I saved the imshow plots in the .png Format and then I used the avconv command to stitch the images together. 为此,首先将imshow图保存为.png格式,然后使用avconv命令将图像拼接在一起。 unfortunately, my code creates an empty movie with no scene. 不幸的是,我的代码创建了没有场景的空电影。 now I have 2 questions: 现在我有两个问题:
1- would anyone please help me how can I finally create a movie with this code. 1-谁能帮助我,我最终如何使用此代码创建电影。
2- is there any method that without saving the figures, directly I create a movie?? 2-有什么方法可以在不保存图形的情况下直接创建电影?
here is the code: 这是代码:
import os
import sys #exit the code at a specific line
import subprocess
import sdf
import numpy as np
import matplotlib.pyplot as plt
#import time
#import matplotlib.animation as animation
#from IPython import display
from matplotlib.font_manager import FontProperties
fp = FontProperties('Symbola')
##################### information from EPOCH input.deck
nx,ny= 1200, 1600
xmin=-100e-6
xmax = 110e-6
ymin = -200e-6
ymax = 200e-6
X =np.linspace(xmin,xmax,nx)
Y =np.linspace(ymin,ymax,ny)
#################
for n in range(0,26):
nstr = str(n)#.zfill(4)
#print nstr
######################..... reading Density of Proton
filename ="Den_pro_resample" +'_sdf_'+ str(n)+'.dat'
with open(filename, 'rb') as f: #read binary file
data = np.fromfile(f, dtype='float64', count=nx*ny)
Den_pro = np.reshape(data, [ny, nx], order='F')
Den_pro = np.log10(Den_pro )
########################## Display proton density
plt.subplot(312)
fig2 = plt.imshow(Den_pro, extent=[X.min()*1e6, X.max()*1e6, Y.min()*1e6, Y.max()*1e6], vmin=24, vmax=30, cmap='brg', aspect='auto')
plt.xlabel('x($\mu$m)')
plt.ylabel('y($\mu$m)')
plt.text(-80,-40,'Den_proton',color='red', fontsize=15)
plt.colorbar()
#plt.savefig( 'fig%04d.png' % n, bbox_inches='tight') #('test'+ str(n)+ '.png')
plt.pause(.1)
plt.clf()
############ create a movie
avconv -framerate 1 -i fig%04d.png -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p movie.mp4".split()
#ffmpeg -r 10 -i fig%04d.png -pix_fmt yuv420p movie.mp4".split()
You can use matplotlib
's animation
: 您可以使用matplotlib
的animation
:
#the other imports you need should also be included.
import matplotlib.animation as animation
#create these
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro', animated=True)
#and define this
def update(frame):
#set xdata and ydata to be the new values you need (i.e. of the next frame)
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig2, update, 26)
writer = animation.writers['ffmpeg'](fps=30)
ani.save('demo.mp4',writer=writer,dpi=dpi)
and this would save your data to demo.mp4
. 这会将您的数据保存到demo.mp4
。
You can look at the documentation here for more details. 您可以在此处查看文档以了解更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.