• 05 Python Numpy moivepy 生成MP4 爱满一颗心


    七夕就要来了,不论你是不是一个人,都祝你七夕快乐!!!!

    前提:

    下载moviepy,说实话我下载有点久

    pip install moviepy

    在运行前切记切记:

    运行完在文件夹中会看到生成的结果fullheart.mp4  的文件,切记不要在pycharm中点开,不然你后面再到文件夹中打开这个文件就受损了

    代码:

    1. import urllib.request
    2. import numpy as np
    3. from scipy.ndimage.filters import convolve
    4. import moviepy.editor as mpy
    5. # 下载心形脉络图和文字
    6. filename = ("https://live.staticflickr.com/65535/51375413098_6835046704_o.png")
    7. urllib.request.urlretrieve(filename, "fullheart.png")
    8. filename = ("https://live.staticflickr.com/65535/51376189625_5ce9caa671_o.png")
    9. urllib.request.urlretrieve(filename, "fullwords.png")
    10. """
    11. * @Author: xiaofang
    12. * @Description:
    13. 学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:732481539
    14. 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
    15. """
    16. #### 参数和约束条件
    17. infection_rate = 0.4
    18. incubation_rate = 0.1
    19. dispersion_rates = [0, 0.07, 0.03] # for S, I, R
    20. # 该内核会模拟病毒如何用一个位置扩散至邻近位置
    21. dispersion_kernel = np.array([[0.5, 1, 0.5],
    22. [1, -6, 1],
    23. [0.5, 1, 0.5]])
    24. heart = mpy.ImageClip("fullheart.png").resize(width=400)
    25. words = mpy.ImageClip("fullwords.png").resize(width=400) # 后面在渲染的时候直接把文字矩阵与心形图矩阵相加
    26. SIR = np.zeros((3, heart.h, heart.w), dtype=float)
    27. SIR[0] = heart.get_frame(0).mean(axis=2) / 255
    28. WORDS = words.get_frame(0)
    29. start = int(0.4 * heart.h), int(0.611 * heart.w) # 在这里修改最开始感染的位置
    30. SIR[1, start[0], start[1]] = 0.8 # infection in Grenoble at t=0
    31. dt = 1.0 # 一次更新=实时1个小时
    32. hours_per_second = 7 * 24 # one second in the video = one week in the model
    33. world = {'SIR': SIR, 't': 0}
    34. ##### 建模
    35. def infection(SIR, infection_rate, incubation_rate):
    36. """ Computes the evolution of #Sane, #Infected, #Rampaging"""
    37. S, I, R = SIR
    38. newly_infected = infection_rate * R * S
    39. newly_rampaging = incubation_rate * I
    40. dS = - newly_infected
    41. dI = newly_infected - newly_rampaging
    42. dR = newly_rampaging
    43. return np.array([dS, dI, dR])
    44. def dispersion(SIR, dispersion_kernel, dispersion_rates):
    45. """ Computes the dispersion (spread) of people """
    46. return np.array([convolve(e, dispersion_kernel, cval=0) * r
    47. for (e, r) in zip(SIR, dispersion_rates)])
    48. def update(world):
    49. """ spread the epidemic for one time step """
    50. infect = infection(world['SIR'], infection_rate, incubation_rate)
    51. disperse = dispersion(world['SIR'], dispersion_kernel, dispersion_rates)
    52. world['SIR'] += dt * (infect + disperse)
    53. world['t'] += dt
    54. # 用MoviePy制作动画
    55. def world_to_npimage(world):
    56. """ Converts the world's map into a RGB image for the final video."""
    57. coefs = np.array([2, 25, 25]).reshape((3, 1, 1))
    58. accentuated_world = 255 * coefs * world['SIR']
    59. image = accentuated_world[::-1].swapaxes(0, 2).swapaxes(0, 1)
    60. return np.minimum(255, image) + WORDS # 加上文字
    61. def make_frame(t):
    62. """ Return the frame for time t """
    63. while world['t'] < hours_per_second * t:
    64. update(world)
    65. return world_to_npimage(world)
    66. animation = mpy.VideoClip(make_frame, duration=16)
    67. # 可以将结果写为视频或GIF(速度较慢),每次选择一个格式渲染
    68. # animation.write_gif('fullheart.gif', fps=30)
    69. animation.write_videofile('fullheart.mp4', fps=30)

     

  • 相关阅读:
    python+nodejs+vue考研辅导网站系统
    Pr:创建自己的项目模板
    tensorflow卷积层操作
    Vue学习第21天——插槽slot的理解及案例
    对程序、进程、线程、并发、并行、高并发概念的讲解
    CEC2013(MATLAB):霸王龙优化算法(Tyrannosaurus optimization)求解CEC2013
    甘特图组件DHTMLX Gantt示例 - 如何有效管理团队工作时间?(二)
    [MDM9607]高通9607 QCMAP设置LAN IP之后无法获取到IP地址问题分析及解决方案
    基于SVM+TensorFlow+Django的酒店评论打分智能推荐系统——机器学习算法应用(含python工程源码)+数据集+模型(二)
    LLM推理入门指南②:深入解析KV缓存
  • 原文地址:https://blog.csdn.net/c_lanxiaofang/article/details/126111484