From d3876bc95dd7491e9b74b2a231252397f681cef0 Mon Sep 17 00:00:00 2001 From: feilong Date: Thu, 30 Dec 2021 09:57:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E7=94=9F=E4=BA=A7GIF?= =?UTF-8?q?=EF=BC=8C=E5=86=8D=E5=90=88=E5=B9=B6=E6=88=90MP4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 6 ++++-- requirements.txt | 5 +++++ src/bar_chart_race/chart.py | 10 ++++++++-- src/common/gif.py | 9 +++++++++ src/top.py | 30 +++++++++++++++++++++++++++--- 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/common/gif.py diff --git a/main.py b/main.py index 1bb0c9b..8f60c40 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,8 @@ def test_build_csdn_ask_top10_tag_race(): ) output = OutputMeta( - path='pub/csdn_ask_top10_month.gif', + path='pub/csdn_ask_top10_month', + ext='gif', title='CSDN 问答标签月排行榜', x_label='ask.csdn.net', y_label='问题数', @@ -32,7 +33,8 @@ def test_build_csdn_trend_top10_tag_race(): ) output = OutputMeta( - path='pub/csdn_trends_top10_month.mp4', + path='pub/csdn_trends_top10_month', + ext='gif', title='CSDN topN指数月排行榜', x_label='csdn.net/trends', y_label='指数', diff --git a/requirements.txt b/requirements.txt index e69de29..236964b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,5 @@ +progress==1.5 +matplotlib==3.4.2 +pandas==1.2.4 +numpy==1.19.5 +moviepy==1.0.3 diff --git a/src/bar_chart_race/chart.py b/src/bar_chart_race/chart.py index 1883762..5e7caca 100644 --- a/src/bar_chart_race/chart.py +++ b/src/bar_chart_race/chart.py @@ -1,7 +1,9 @@ +import sys import warnings import platform import pandas as pd import numpy as np +from progress.bar import IncrementalBar import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation @@ -413,7 +415,7 @@ class BarChartRace: i += 1 def anim_func(self, i): - print('anim_func:', i) + self.bar.next(1) plt.xkcd(scale=1, length=100, randomness=2) if(platform.system() == 'Linux'): plt.rcParams.update({'font.family': "WenQuanYi Micro Hei"}) @@ -430,13 +432,15 @@ class BarChartRace: def init_func(): self.plot_bars(0) - print('total:', len(self.df_values)) + total_count = len(self.df_values) + self.bar = IncrementalBar(self.filename, max=total_count) interval = self.period_length / self.steps_per_period anim = FuncAnimation(self.fig, self.anim_func, range(len(self.df_values)), init_func, interval=interval) try: if self.html: + print("@anim.to_html5_video...") ret_val = anim.to_html5_video() try: from IPython.display import HTML @@ -444,6 +448,7 @@ class BarChartRace: except ImportError: pass else: + print(f"@anim.save({self.filename})") ret_val = anim.save( self.filename, fps=self.fps, writer=self.writer) except Exception as e: @@ -459,6 +464,7 @@ class BarChartRace: raise Exception(message) finally: plt.rcParams = self.orig_rcParams + self.bar.finish() return ret_val diff --git a/src/common/gif.py b/src/common/gif.py new file mode 100644 index 0000000..d614d70 --- /dev/null +++ b/src/common/gif.py @@ -0,0 +1,9 @@ +import moviepy.editor as mp + +def concat_gif_list(gifs, output): + clips = [] + for gif in gifs: + clips.append(mp.VideoFileClip(gif)) + final = mp.concatenate_videoclips(clips) + final.write_videofile(output) + diff --git a/src/top.py b/src/top.py index b37bb33..373144d 100644 --- a/src/top.py +++ b/src/top.py @@ -6,6 +6,7 @@ import matplotlib from src.common.path import ordered_list_json_dir from src.common.json import load_json, dump_json +from src.common.gif import concat_gif_list from src.bar_chart_race.chart import bar_chart_race from dataclasses import dataclass @@ -41,6 +42,7 @@ class OutputMeta: month_count: 绘制月份,用来调试,使用较少的月份快速查看输出效果 ''' path: str + ext: str title: str x_label: str y_label: str @@ -60,6 +62,7 @@ class Top: self.count_field = input.count_field self.output = output.path + self.ext = output.ext self.title = output.title self.x_label = output.x_label self.y_label = output.y_label @@ -69,7 +72,28 @@ class Top: self.__load_dataframe() # self.__exit() self.__config_font() - self.__build_race() + + max_rows = self.df.shape[0] + i = 0 + j = 0 + df = self.df + gifs = [] + os.makedirs(self.output, exist_ok=True) + while i < max_rows: + end = i+12 + if end >= max_rows: + end = max_rows+1 + step = end-i + self.df = df[i:end] + + filename = os.path.join(self.output, f'{j}.{self.ext}') + self.__build_race(filename) + gifs.append(filename) + i += step + j += 1 + + all = f'{self.output}.mp4' + # concat_gif_list(gifs, all) def __load_dataframe(self): if self.input_type == 'json_dir': @@ -143,10 +167,10 @@ class Top: else: matplotlib.rc("font", family='FZFangSong-Z02') - def __build_race(self): + def __build_race(self, filename): bar_chart_race( df=self.df, - filename=self.output, + filename=filename, n_bars=10, title=self.title, title_size=24, -- GitLab