Spaces:
Running
Running
Create modules/utils.py
Browse files- modules/utils.py +66 -0
modules/utils.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
from PIL import ImageFont
|
| 5 |
+
|
| 6 |
+
def parse_color(color_str, default=(255, 255, 255)):
|
| 7 |
+
color_map = {
|
| 8 |
+
"gold": (255, 215, 0), "red": (255, 60, 60), "blue": (60, 120, 255),
|
| 9 |
+
"white": (255, 255, 255), "black": (0, 0, 0), "cyan": (0, 255, 255),
|
| 10 |
+
"magenta": (255, 0, 255), "yellow": (255, 255, 0), "orange": (255, 165, 0),
|
| 11 |
+
"pink": (255, 105, 180), "lime": (50, 255, 50),
|
| 12 |
+
}
|
| 13 |
+
if color_str.lower() in color_map:
|
| 14 |
+
return color_map[color_str.lower()]
|
| 15 |
+
if color_str.startswith("#") and len(color_str) == 7:
|
| 16 |
+
try:
|
| 17 |
+
return (int(color_str[1:3], 16), int(color_str[3:5], 16), int(color_str[5:7], 16))
|
| 18 |
+
except:
|
| 19 |
+
pass
|
| 20 |
+
return default
|
| 21 |
+
|
| 22 |
+
def get_chinese_font():
|
| 23 |
+
fonts = [
|
| 24 |
+
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
|
| 25 |
+
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
| 26 |
+
"/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
|
| 27 |
+
"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
|
| 28 |
+
"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
|
| 29 |
+
"NotoSansCJK-Regular.ttc",
|
| 30 |
+
]
|
| 31 |
+
for f in fonts:
|
| 32 |
+
if os.path.exists(f):
|
| 33 |
+
return f
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
def get_title_font(font_path, size):
|
| 37 |
+
try:
|
| 38 |
+
return ImageFont.truetype(font_path, size) if font_path else ImageFont.load_default()
|
| 39 |
+
except:
|
| 40 |
+
return ImageFont.load_default()
|
| 41 |
+
|
| 42 |
+
def fit_image_to_canvas(img, target_w, target_h):
|
| 43 |
+
h, w = img.shape[:2]
|
| 44 |
+
scale = min(target_w / w, target_h / h)
|
| 45 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 46 |
+
resized = cv2.resize(img, (new_w, new_h))
|
| 47 |
+
canvas = np.zeros((target_h, target_w, 3), dtype=np.uint8)
|
| 48 |
+
x_offset, y_offset = (target_w - new_w) // 2, (target_h - new_h) // 2
|
| 49 |
+
canvas[y_offset:y_offset + new_h, x_offset:x_offset + new_w] = resized
|
| 50 |
+
return canvas
|
| 51 |
+
|
| 52 |
+
def ken_burns_crop(img, progress, w_out, h_out):
|
| 53 |
+
h, w = img.shape[:2]
|
| 54 |
+
zoom = 1.0 + 0.15 * progress
|
| 55 |
+
crop_w, crop_h = w_out / zoom, h_out / zoom
|
| 56 |
+
dx = max(0, min((w - crop_w) * progress, w - crop_w)) if w > crop_w else 0
|
| 57 |
+
dy = max(0, min((h - crop_h) * progress, h - crop_h)) if h > crop_h else 0
|
| 58 |
+
crop = img[int(dy):int(dy + crop_h), int(dx):int(dx + crop_w)]
|
| 59 |
+
return cv2.resize(crop, (w_out, h_out))
|
| 60 |
+
|
| 61 |
+
def apply_cinemascope(frame, bar_height_ratio=0.08):
|
| 62 |
+
h = frame.shape[0]
|
| 63 |
+
bar_h = int(h * bar_height_ratio)
|
| 64 |
+
frame[:bar_h, :] = 0
|
| 65 |
+
frame[h - bar_h:, :] = 0
|
| 66 |
+
return frame
|