2017-05-23 77 views
0

我有一些名稱爲('Den_pro_resample'+'_sdf _'+ str(n)+'.dat')的二進制文件,其中n從0改變爲25.
我寫了一個代碼來讀取這些文件,然後用imshow命令繪製結果
在最後一步,我想創建一個電影。首先,我將imshow圖保存爲.png格式,然後使用avconv命令拼接圖像 不幸的是,我的代碼創建一個空的影片沒有現場現在我有2個問題:。如何用python製作電影

1-會有人請幫助我,我怎麼能最終建立一個電影與此代碼

2-是否有任何方法,不保存數字,直接我創建一個電影?

這裏是代碼:

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() 
+0

你的代碼已經在「sdf」附近加下劃線,但是你頂部的語句沒有。哪個是對的? – anonymoose

+0

你有工作嗎? – ItamarG3

回答

0

您可以使用matplotlibanimation

#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) 

,這將您的數據保存到demo.mp4

有關更多詳細信息,請參閱文檔here