Spaces:
Sleeping
Sleeping
Update generators/base.py
Browse files- generators/base.py +20 -5
generators/base.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# utils/generators/base.py
|
| 2 |
import io
|
| 3 |
import os
|
|
|
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
|
| 6 |
class BaseGenerator:
|
|
@@ -22,12 +23,26 @@ class BaseGenerator:
|
|
| 22 |
|
| 23 |
@staticmethod
|
| 24 |
def get_font(size: int):
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
except
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
@staticmethod
|
| 33 |
def create_rounded_mask(size, radius):
|
|
|
|
| 1 |
# utils/generators/base.py
|
| 2 |
import io
|
| 3 |
import os
|
| 4 |
+
import urllib.request
|
| 5 |
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
|
| 7 |
class BaseGenerator:
|
|
|
|
| 23 |
|
| 24 |
@staticmethod
|
| 25 |
def get_font(size: int):
|
| 26 |
+
# 🛡️ THE BULLETPROOF FONT FIX
|
| 27 |
+
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 28 |
+
assets_dir = os.path.join(base_dir, "assets")
|
| 29 |
+
os.makedirs(assets_dir, exist_ok=True)
|
| 30 |
+
font_path = os.path.join(assets_dir, "font.ttf")
|
| 31 |
+
|
| 32 |
+
# If font is missing OR corrupted (< 10KB), download a fresh one automatically
|
| 33 |
+
if not os.path.exists(font_path) or os.path.getsize(font_path) < 10000:
|
| 34 |
+
try:
|
| 35 |
+
print("Font missing or corrupted! Downloading fresh font...")
|
| 36 |
+
url = "https://github.com/google/fonts/raw/main/ofl/roboto/Roboto-Bold.ttf"
|
| 37 |
+
urllib.request.urlretrieve(url, font_path)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Download failed: {e}")
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
+
return ImageFont.truetype(font_path, size)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Pillow failed to load font: {e}")
|
| 45 |
+
return ImageFont.load_default()
|
| 46 |
|
| 47 |
@staticmethod
|
| 48 |
def create_rounded_mask(size, radius):
|