1313import hashlib
1414import numpy as np
1515import matplotlib .pyplot as plt
16+ import joblib
1617from PIL import Image , ImageDraw , ImageFont
1718from AnimatedWordCloud .Utils import (
1819 ensure_directory_exists ,
@@ -58,7 +59,7 @@ def create_image(
5859 frame_number : int ,
5960 time_name : str ,
6061 color_func = None ,
61- ) -> str :
62+ ) -> tuple [ int , str ] :
6263 """
6364 Create image of a frame
6465
@@ -67,8 +68,8 @@ def create_image(
6768 :param int frame_number: Number of the frame. Used for filename
6869 :param str time_name: Name of the time. Used for time stamp
6970 :param object color_func: Custom function for color mapping, default is None.
70- :return: The path of the image.
71- :rtype: str
71+ :return: (frame_number, save_path)
72+ :rtype: tuple[int, str]
7273 """
7374 if color_func is None :
7475 color_func = colormap_color_func (config .color_map )
@@ -109,7 +110,7 @@ def create_image(
109110 save_path = os .path .join (config .output_path , filename )
110111 image .save (save_path )
111112
112- return save_path
113+ return ( frame_number , save_path )
113114
114115
115116def create_images (
@@ -127,22 +128,35 @@ def create_images(
127128 :rtype: list[str]
128129 """
129130
131+ if config .verbosity in ["debug" ]:
132+ print ("Creating images of each frame..." )
133+
130134 ensure_directory_exists (config .output_path )
131135
132136 image_paths = []
133137
134- frame_number = 0
135- for time_name , allocation_in_frame in position_in_frames .timelapse :
136- save_path = create_image (
138+ verbosity = 0
139+ if config .verbosity in ["debug" ]:
140+ verbosity = 5
141+
142+ # create images of each frame
143+ result = joblib .Parallel (n_jobs = - 1 , verbose = verbosity )(
144+ joblib .delayed (create_image )(
137145 allocation_in_frame = allocation_in_frame ,
138146 config = config ,
139147 frame_number = frame_number ,
140148 color_func = color_func ,
141149 time_name = time_name ,
142150 )
151+ for frame_number , (time_name , allocation_in_frame ) in enumerate (
152+ position_in_frames .timelapse
153+ )
154+ )
143155
144- image_paths .append (save_path )
156+ # sort by frame number (ascending)
157+ result .sort (key = lambda x : x [0 ])
145158
146- frame_number += 1
159+ # get only the path
160+ image_paths = [path for _ , path in result ]
147161
148162 return image_paths
0 commit comments