File size: 2,462 Bytes
d3d4310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import os
from PIL import ImageFont

def parse_color(color_str, default=(255, 255, 255)):
    color_map = {
        "gold": (255, 215, 0), "red": (255, 60, 60), "blue": (60, 120, 255),
        "white": (255, 255, 255), "black": (0, 0, 0), "cyan": (0, 255, 255),
        "magenta": (255, 0, 255), "yellow": (255, 255, 0), "orange": (255, 165, 0),
        "pink": (255, 105, 180), "lime": (50, 255, 50),
    }
    if color_str.lower() in color_map:
        return color_map[color_str.lower()]
    if color_str.startswith("#") and len(color_str) == 7:
        try:
            return (int(color_str[1:3], 16), int(color_str[3:5], 16), int(color_str[5:7], 16))
        except:
            pass
    return default

def get_chinese_font():
    fonts = [
        "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
        "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",
        "NotoSansCJK-Regular.ttc",
    ]
    for f in fonts:
        if os.path.exists(f):
            return f
    return None

def get_title_font(font_path, size):
    try:
        return ImageFont.truetype(font_path, size) if font_path else ImageFont.load_default()
    except:
        return ImageFont.load_default()

def fit_image_to_canvas(img, target_w, target_h):
    h, w = img.shape[:2]
    scale = min(target_w / w, target_h / h)
    new_w, new_h = int(w * scale), int(h * scale)
    resized = cv2.resize(img, (new_w, new_h))
    canvas = np.zeros((target_h, target_w, 3), dtype=np.uint8)
    x_offset, y_offset = (target_w - new_w) // 2, (target_h - new_h) // 2
    canvas[y_offset:y_offset + new_h, x_offset:x_offset + new_w] = resized
    return canvas

def ken_burns_crop(img, progress, w_out, h_out):
    h, w = img.shape[:2]
    zoom = 1.0 + 0.15 * progress
    crop_w, crop_h = w_out / zoom, h_out / zoom
    dx = max(0, min((w - crop_w) * progress, w - crop_w)) if w > crop_w else 0
    dy = max(0, min((h - crop_h) * progress, h - crop_h)) if h > crop_h else 0
    crop = img[int(dy):int(dy + crop_h), int(dx):int(dx + crop_w)]
    return cv2.resize(crop, (w_out, h_out))

def apply_cinemascope(frame, bar_height_ratio=0.08):
    h = frame.shape[0]
    bar_h = int(h * bar_height_ratio)
    frame[:bar_h, :] = 0
    frame[h - bar_h:, :] = 0
    return frame