程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

05 Python Numpy moivepy 生成MP4 愛滿一顆心

編輯:Python

七夕就要來了,不論你是不是一個人,都祝你七夕快樂!!!!

前提:

下載moviepy,說實話我下載有點久

pip install moviepy

在運行前切記切記:

運行完在文件夾中會看到生成的結果fullheart.mp4  的文件,切記不要在pycharm中點開,不然你後面再到文件夾中打開這個文件就受損了

代碼:

import urllib.request
import numpy as np
from scipy.ndimage.filters import convolve
import moviepy.editor as mpy
# 下載心形脈絡圖和文字
filename = ("https://live.staticflickr.com/65535/51375413098_6835046704_o.png")
urllib.request.urlretrieve(filename, "fullheart.png")
filename = ("https://live.staticflickr.com/65535/51376189625_5ce9caa671_o.png")
urllib.request.urlretrieve(filename, "fullwords.png")
"""
* @Author: xiaofang
* @Description:
學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:732481539
尋找有志同道合的小伙伴,互幫互助,群裡還有不錯的視頻學習教程和PDF電子書!
"""
#### 參數和約束條件
infection_rate = 0.4
incubation_rate = 0.1
dispersion_rates = [0, 0.07, 0.03] # for S, I, R
# 該內核會模擬病毒如何用一個位置擴散至鄰近位置
dispersion_kernel = np.array([[0.5, 1, 0.5],
[1, -6, 1],
[0.5, 1, 0.5]])
heart = mpy.ImageClip("fullheart.png").resize(width=400)
words = mpy.ImageClip("fullwords.png").resize(width=400) # 後面在渲染的時候直接把文字矩陣與心形圖矩陣相加
SIR = np.zeros((3, heart.h, heart.w), dtype=float)
SIR[0] = heart.get_frame(0).mean(axis=2) / 255
WORDS = words.get_frame(0)
start = int(0.4 * heart.h), int(0.611 * heart.w) # 在這裡修改最開始感染的位置
SIR[1, start[0], start[1]] = 0.8 # infection in Grenoble at t=0
dt = 1.0 # 一次更新=實時1個小時
hours_per_second = 7 * 24 # one second in the video = one week in the model
world = {'SIR': SIR, 't': 0}
##### 建模
def infection(SIR, infection_rate, incubation_rate):
""" Computes the evolution of #Sane, #Infected, #Rampaging"""
S, I, R = SIR
newly_infected = infection_rate * R * S
newly_rampaging = incubation_rate * I
dS = - newly_infected
dI = newly_infected - newly_rampaging
dR = newly_rampaging
return np.array([dS, dI, dR])
def dispersion(SIR, dispersion_kernel, dispersion_rates):
""" Computes the dispersion (spread) of people """
return np.array([convolve(e, dispersion_kernel, cval=0) * r
for (e, r) in zip(SIR, dispersion_rates)])
def update(world):
""" spread the epidemic for one time step """
infect = infection(world['SIR'], infection_rate, incubation_rate)
disperse = dispersion(world['SIR'], dispersion_kernel, dispersion_rates)
world['SIR'] += dt * (infect + disperse)
world['t'] += dt
# 用MoviePy制作動畫
def world_to_npimage(world):
""" Converts the world's map into a RGB image for the final video."""
coefs = np.array([2, 25, 25]).reshape((3, 1, 1))
accentuated_world = 255 * coefs * world['SIR']
image = accentuated_world[::-1].swapaxes(0, 2).swapaxes(0, 1)
return np.minimum(255, image) + WORDS # 加上文字
def make_frame(t):
""" Return the frame for time t """
while world['t'] < hours_per_second * t:
update(world)
return world_to_npimage(world)
animation = mpy.VideoClip(make_frame, duration=16)
# 可以將結果寫為視頻或GIF(速度較慢),每次選擇一個格式渲染
# animation.write_gif('fullheart.gif', fps=30)
animation.write_videofile('fullheart.mp4', fps=30)

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved