-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.py
More file actions
275 lines (237 loc) · 11.2 KB
/
renderer.py
File metadata and controls
275 lines (237 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import os
import cv2
import numpy as np
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
from PIL import Image, ImageDraw, ImageFont
from tqdm import tqdm
# Global variable in each worker process to store the font
_worker_font = None
_worker_font_size = None
def _worker_init(font_size):
"""Initializer function for worker processes."""
global _worker_font, _worker_font_size
_worker_font_size = font_size
# Load font (need to load it here since we can't pickle the font object)
font = None
# List of Japanese fonts to try on Windows, in order of preference
font_paths = [
"C:\\Windows\\Fonts\\msgothic.ttc", # MS Gothic (common Japanese font on Windows)
"C:\\Windows\\Fonts\\YuGothR.ttc", # Yu Gothic Regular
"C:\\Windows\\Fonts\\meiryo.ttc", # Meiryo
"C:\\Windows\\Fonts\\meiryob.ttc", # Meiryo Bold
"C:\\Windows\\Fonts\\msmincho.ttc", # MS Mincho
"C:\\Windows\\Fonts\\yugothib.ttf", # Yu Gothic Bold
"C:\\Windows\\Fonts\\malgun.ttf" # Malgun Gothic (Korean, but has some Japanese support)
]
# Try to load each font until one succeeds
for path in font_paths:
if os.path.exists(path):
try:
font = ImageFont.truetype(path, font_size)
break
except IOError:
continue
# If no font was loaded, try to use a default font
if font is None:
try:
font = ImageFont.truetype("C:\\Windows\\Fonts\\arial.ttf", font_size)
except IOError:
font = ImageFont.load_default()
_worker_font = font
class Renderer:
def __init__(self, font_size=12, fps=30, num_processes=None):
self.font_size = font_size
self.fps = fps
self.num_processes = num_processes if num_processes is not None else os.cpu_count()
self._load_font()
def _load_font(self):
"""
Load a Japanese font for rendering on Windows.
Tries several common Japanese fonts and falls back to a default if none are available.
"""
# List of Japanese fonts to try on Windows, in order of preference
font_paths = [
"C:\\Windows\\Fonts\\msgothic.ttc", # MS Gothic (common Japanese font on Windows)
"C:\\Windows\\Fonts\\YuGothR.ttc", # Yu Gothic Regular
"C:\\Windows\\Fonts\\meiryo.ttc", # Meiryo
"C:\\Windows\\Fonts\\meiryob.ttc", # Meiryo Bold
"C:\\Windows\\Fonts\\msmincho.ttc", # MS Mincho
"C:\\Windows\\Fonts\\yugothib.ttf", # Yu Gothic Bold
"C:\\Windows\\Fonts\\malgun.ttf" # Malgun Gothic (Korean, but has some Japanese support)
]
# Try to load each font until one succeeds
self.font = None
loaded_font_path = None
for font_path in font_paths:
if os.path.exists(font_path):
try:
self.font = ImageFont.truetype(font_path, self.font_size)
loaded_font_path = font_path
break
except IOError:
continue
# If no font was loaded, try to use a default font
if self.font is None:
try:
# Try to use Arial as a last resort (though it won't display Japanese properly)
self.font = ImageFont.truetype("C:\\Windows\\Fonts\\arial.ttf", self.font_size)
print("Warning: Could not load a Japanese font. Using Arial instead.")
print("Japanese characters may not display properly.")
except IOError:
# Last resort: use default
self.font = ImageFont.load_default()
print("Warning: Could not load any font. Using default font.")
print("Japanese characters may not display properly.")
else:
print(f"Using font: {os.path.basename(loaded_font_path)}")
def render_ascii_frames(self, ascii_frames, output_path, actual_dimensions=None, batch_size=10):
"""
Render ASCII frames to an output video using parallel processing.
Args:
ascii_frames (list): List of 2D ASCII frames
output_path (str): Path to save the output video
actual_dimensions (tuple, optional): Actual width and height of ASCII frames
(for information purposes only)
batch_size (int): Number of frames to process in each batch
"""
if not ascii_frames:
raise ValueError("No ASCII frames to render")
# Determine dimensions from the actual ASCII frames
# (actual_dimensions parameter is for information only)
height = len(ascii_frames[0])
width = len(ascii_frames[0][0])
# Log the dimensions being used
print(f"Rendering ASCII frames with dimensions: {width}x{height}")
print(f"Using {self.num_processes} processes for parallel rendering")
# Calculate output image dimensions
char_width = self.font_size
char_height = self.font_size
img_width = width * char_width
img_height = height * char_height
# Set up temporary directory for frames
temp_dir = os.path.join(os.path.dirname(output_path), "temp_frames")
os.makedirs(temp_dir, exist_ok=True)
try:
# Prepare frame rendering tasks
frame_paths = []
render_tasks = []
for i, ascii_frame in enumerate(ascii_frames):
frame_path = os.path.join(temp_dir, f"frame_{i:06d}.png")
frame_paths.append(frame_path)
# Removed font_size from task tuple
render_tasks.append((ascii_frame, frame_path, img_width, img_height))
# Render frames in parallel using ProcessPoolExecutor
# Pass initializer and initargs
with ProcessPoolExecutor(max_workers=self.num_processes,
initializer=_worker_init,
initargs=(self.font_size,)) as executor: # Pass font_size to initializer
# Process frames in batches to manage memory
for batch_start in tqdm(range(0, len(render_tasks), batch_size), desc="Rendering frame batches"):
batch_end = min(batch_start + batch_size, len(render_tasks))
batch_tasks = render_tasks[batch_start:batch_end]
# Submit batch for parallel processing
futures = [
executor.submit(
render_ascii_frame_to_image_static,
task[0], # ascii_frame
task[1], # frame_path
task[2], # img_width
task[3] # img_height
# Removed font_size argument
) for task in batch_tasks
]
# Wait for all futures to complete
for future in futures:
try:
future.result()
except Exception as e:
print(f"Error rendering frame: {e}")
# Use OpenCV to create the video
self._create_video_from_frames(frame_paths, output_path, img_width, img_height)
finally:
# Clean up temporary frames
import shutil
try:
shutil.rmtree(temp_dir, ignore_errors=True)
except Exception as e:
print(f"Warning: Could not remove temporary directory: {temp_dir}")
print(f"Error: {str(e)}")
print("You may want to delete it manually.")
def _render_ascii_frame_to_image(self, ascii_frame, output_path, img_width, img_height):
"""
Render a single ASCII frame to an image.
Args:
ascii_frame (list): 2D list of ASCII characters
output_path (str): Path to save the rendered image
img_width (int): Width of the output image
img_height (int): Height of the output image
"""
# Create a black image
image = Image.new("RGB", (img_width, img_height), color="black")
draw = ImageDraw.Draw(image)
# Draw each character
for y, row in enumerate(ascii_frame):
for x, char in enumerate(row):
# Draw white character on black background
draw.text(
(x * self.font_size, y * self.font_size),
char,
font=self.font,
fill="white"
)
# Save the image
image.save(output_path)
def _create_video_from_frames(self, frame_paths, output_path, width, height):
"""
Create a video from a list of frame image paths.
Args:
frame_paths (list): List of paths to frame images
output_path (str): Path to save the output video
width (int): Width of the video
height (int): Height of the video
"""
# Ensure output directory exists
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
# For Windows compatibility, use mp4v codec
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(output_path, fourcc, self.fps, (width, height))
if not video_writer.isOpened():
raise RuntimeError(f"Could not create video writer for {output_path}")
for frame_path in tqdm(frame_paths, desc="Creating video"):
frame = cv2.imread(frame_path)
if frame is None:
print(f"Warning: Could not read frame {frame_path}")
continue
video_writer.write(frame)
video_writer.release()
print(f"Video saved to {output_path}")
# Static method for parallel processing
def render_ascii_frame_to_image_static(ascii_frame, output_path, img_width, img_height):
"""
Static method to render a single ASCII frame to an image.
This is used for parallel processing since instance methods can't be pickled.
Uses the font loaded in the worker initializer.
Args:
ascii_frame (list): 2D list of ASCII characters
output_path (str): Path to save the rendered image
img_width (int): Width of the output image
img_height (int): Height of the output image
# Removed font_size as it's now in the global _worker_font_size
"""
global _worker_font, _worker_font_size
# Create a black image
image = Image.new("RGB", (img_width, img_height), color="black")
draw = ImageDraw.Draw(image)
# Draw each character
for y, row in enumerate(ascii_frame):
for x, char in enumerate(row):
# Draw white character on black background
draw.text(
(x * _worker_font_size, y * _worker_font_size), # Use global font size
char,
font=_worker_font, # Use global font
fill="white"
)
# Save the image
image.save(output_path)