Skip to content

Commit 25e4ace

Browse files
author
arch
committed
make text more readable
1 parent 1f24cb7 commit 25e4ace

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

funscript_editor/ui/opencvui.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ class OpenCV_GUI_Parameters:
2222
skip_frames: int
2323
end_frame_number: int
2424
preview_scaling: float = float(SETTINGS['preview_scaling'])
25-
text_start_x: int = 40
26-
text_start_y: int = 40
27-
text_line_height: int = 28
25+
text_start_x: int = 30
26+
text_start_y: int = 30
27+
text_line_height: int = 30
2828
font_size: float = 0.75
2929
fps_smoothing_factor: int = 100
3030
window_name_prefix: str = "MTFG"
3131
notification_sound_file = NOTIFICATION_SOUND_FILE
3232
zoom_factor: float = max((1.0, float(SETTINGS['zoom_factor'])))
33+
text_border_width: int = 6
3334

3435

3536
class KeypressHandler:
@@ -285,7 +286,7 @@ def print_time(self, current_frame_number: int) -> None:
285286
self.print_text(txt, text_position_x = 'right')
286287

287288

288-
def print_text(self, txt, color: tuple = (0,0,255), text_position_x: str = 'left') -> None:
289+
def print_text(self, txt, color: tuple = (243,153,29), text_position_x: str = 'left') -> None:
289290
""" Draw text to an image/frame
290291
291292
Args:
@@ -296,23 +297,32 @@ def print_text(self, txt, color: tuple = (0,0,255), text_position_x: str = 'left
296297
assert self.preview_image is not None
297298
assert text_position_x in self.text_y_pos.keys()
298299

299-
if text_position_x.lower() == 'left':
300-
x = self.params.text_start_x
301-
elif text_position_x.lower() == 'right':
302-
x = max([0, int(self.preview_image.shape[1] - self.params.text_start_x - round(len(txt)*17*self.params.font_size)) ])
303-
elif text_position_x.lower() == 'center':
304-
x = round(self.preview_image_origin_width / 2 + self.params.text_start_x)
305-
else:
306-
raise NotImplementedError("Print Text at position %s is not implemented", text_position_x)
307-
308300
if not isinstance(txt, list):
309301
txt = [txt]
310302

311303
for line in txt:
304+
if text_position_x.lower() == 'left':
305+
x = self.params.text_start_x
306+
elif text_position_x.lower() == 'right':
307+
(text_w, _), _ = cv2.getTextSize(line, cv2.FONT_HERSHEY_SIMPLEX, self.params.font_size, 2)
308+
x = max([0, int(self.preview_image.shape[1] - self.params.text_start_x - text_w) ])
309+
elif text_position_x.lower() == 'center':
310+
x = round(self.preview_image_origin_width / 2 + self.params.text_start_x)
311+
else:
312+
raise NotImplementedError("Print Text at position %s is not implemented", text_position_x)
313+
314+
(text_w, text_h), _ = cv2.getTextSize(line, cv2.FONT_HERSHEY_SIMPLEX, self.params.font_size, 2)
315+
cv2.rectangle(
316+
self.preview_image,
317+
(x - self.params.text_border_width, self.text_y_pos[text_position_x] - self.params.text_border_width),
318+
(x + text_w + self.params.text_border_width, self.text_y_pos[text_position_x] + text_h + self.params.text_border_width),
319+
(0, 0, 0),
320+
-1
321+
)
312322
cv2.putText(
313323
self.preview_image,
314324
str(line),
315-
(x, self.text_y_pos[text_position_x]),
325+
(x, round(self.text_y_pos[text_position_x] + text_h + self.params.font_size - 1)),
316326
cv2.FONT_HERSHEY_SIMPLEX,
317327
self.params.font_size,
318328
color,

0 commit comments

Comments
 (0)