Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,6 +65,13 @@ def apply_linear_gain(audio_clip, gain_linear: float):
|
|
| 65 |
# ---------- Image utilities ----------
|
| 66 |
|
| 67 |
def load_and_fit_image(path: str, width: int, height: int, fit: str = "contain", bg: str = "#000000") -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
img = Image.open(path).convert("RGB")
|
| 69 |
|
| 70 |
if fit == "stretch":
|
|
@@ -76,19 +83,22 @@ def load_and_fit_image(path: str, width: int, height: int, fit: str = "contain",
|
|
| 76 |
src_aspect = float(iw) / float(ih)
|
| 77 |
|
| 78 |
if fit == "cover":
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
new_h = height
|
| 81 |
-
new_w = int(round(
|
| 82 |
else:
|
|
|
|
| 83 |
new_w = width
|
| 84 |
-
new_h = int(round(new_w /
|
| 85 |
img = img.resize((new_w, new_h), Image.LANCZOS)
|
| 86 |
left = (new_w - width) // 2
|
| 87 |
top = (new_h - height) // 2
|
| 88 |
img = img.crop((left, top, left + width, top + height))
|
| 89 |
return np.array(img)
|
| 90 |
|
| 91 |
-
# contain
|
| 92 |
canvas = Image.new("RGB", (width, height), bg)
|
| 93 |
if src_aspect > target_aspect:
|
| 94 |
new_w = width
|
|
@@ -103,6 +113,7 @@ def load_and_fit_image(path: str, width: int, height: int, fit: str = "contain",
|
|
| 103 |
return np.array(canvas)
|
| 104 |
|
| 105 |
|
|
|
|
| 106 |
# ---------- TTS backends ----------
|
| 107 |
|
| 108 |
_TTS_CACHE: Dict[str, object] = {}
|
|
|
|
| 65 |
# ---------- Image utilities ----------
|
| 66 |
|
| 67 |
def load_and_fit_image(path: str, width: int, height: int, fit: str = "contain", bg: str = "#000000") -> np.ndarray:
|
| 68 |
+
"""
|
| 69 |
+
Loads an image file and returns an RGB numpy array with exact (height, width, 3).
|
| 70 |
+
fit modes:
|
| 71 |
+
- "contain": letterbox to fit within target size (keeps aspect), background color fills the rest.
|
| 72 |
+
- "cover": fill target size (keeps aspect) with center crop.
|
| 73 |
+
- "stretch": distort to target size.
|
| 74 |
+
"""
|
| 75 |
img = Image.open(path).convert("RGB")
|
| 76 |
|
| 77 |
if fit == "stretch":
|
|
|
|
| 83 |
src_aspect = float(iw) / float(ih)
|
| 84 |
|
| 85 |
if fit == "cover":
|
| 86 |
+
# scale to cover, then center-crop
|
| 87 |
+
if src_aspect > target_aspect:
|
| 88 |
+
# image too wide -> fit height, crop width
|
| 89 |
new_h = height
|
| 90 |
+
new_w = int(round(src_aspect * new_h))
|
| 91 |
else:
|
| 92 |
+
# image too tall -> fit width, crop height
|
| 93 |
new_w = width
|
| 94 |
+
new_h = int(round(new_w / src_aspect))
|
| 95 |
img = img.resize((new_w, new_h), Image.LANCZOS)
|
| 96 |
left = (new_w - width) // 2
|
| 97 |
top = (new_h - height) // 2
|
| 98 |
img = img.crop((left, top, left + width, top + height))
|
| 99 |
return np.array(img)
|
| 100 |
|
| 101 |
+
# contain (letterbox/pillarbox)
|
| 102 |
canvas = Image.new("RGB", (width, height), bg)
|
| 103 |
if src_aspect > target_aspect:
|
| 104 |
new_w = width
|
|
|
|
| 113 |
return np.array(canvas)
|
| 114 |
|
| 115 |
|
| 116 |
+
|
| 117 |
# ---------- TTS backends ----------
|
| 118 |
|
| 119 |
_TTS_CACHE: Dict[str, object] = {}
|