Skip to content

Commit 959bdae

Browse files
committed
2 parents f5b1654 + d39a42f commit 959bdae

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

AnimatedWordCloud/Animator/AnimationIntegrator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def integrate_images(
3030
:rtype: str
3131
"""
3232

33+
if config.verbosity == "debug":
34+
print("Integrating images...")
35+
3336
# input
3437
gif_images = [Image.open(path) for path in image_paths]
3538

AnimatedWordCloud/Animator/ImageCreator.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import hashlib
1414
import numpy as np
1515
import matplotlib.pyplot as plt
16+
import joblib
1617
from PIL import Image, ImageDraw, ImageFont
1718
from 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

115116
def 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

tests/Auto/Animator/Animator_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
less_raw_timelapse = raw_timelapse[:2]
1212

13-
config = Config(max_words=50, max_font_size=20, min_font_size=10)
13+
config = Config(
14+
max_words=50, max_font_size=20, min_font_size=10, verbosity="debug"
15+
)
1416

1517

1618
def test_animate():
17-
animate(less_raw_timelapse, config)
19+
assert animate(less_raw_timelapse, config) != None

tests/Auto/Animator/ImageCreator_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def test_imagecreator():
2525
allocation_in_frame = AllocationInFrame(from_static_allocation=True)
2626
allocation_in_frame.words = {"word": (30, (50, 50))} # dictionary
2727
position_in_frames.add("2023_04_01", allocation_in_frame)
28-
config = Config(intermediate_frames_id="test")
29-
create_images(position_in_frames, config)
28+
config = Config(intermediate_frames_id="test", verbosity="debug")
29+
assert len(create_images(position_in_frames, config)) > 0
3030
test_path = os.path.join(DEFAULT_OUTPUT_PATH, "test_0.png")
3131
assert os.path.isfile(test_path)
3232
os.remove(os.path.join(DEFAULT_OUTPUT_PATH, "test_0.png"))

0 commit comments

Comments
 (0)