2017-05-23 12 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を参照してください。

関連する問題