Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,8 +33,8 @@ class Config:
|
|
| 33 |
# Directories (relative paths for container compatibility)
|
| 34 |
PREDEFINED_TEMPLATES_DIR = "templates"
|
| 35 |
OUTPUT_DIR = "generated_images"
|
| 36 |
-
#
|
| 37 |
-
FONT_PATH = "
|
| 38 |
|
| 39 |
# Predefined Template Settings
|
| 40 |
TEMPLATE_SIZE = (1200, 900) # Expected size of predefined template canvas
|
|
@@ -76,12 +76,9 @@ except OSError as e:
|
|
| 76 |
# If directories cannot be created, the app likely cannot function
|
| 77 |
raise SystemExit(f"FATAL: Cannot create directories {Config.PREDEFINED_TEMPLATES_DIR} or {Config.OUTPUT_DIR}") from e
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
logger.warning(f"Font file specified in Config ('{Config.FONT_PATH}') not found in the container's /app directory. Text rendering might use a system default font if available via fontconfig (installed via Dockerfile), or fail if Pillow finds no fallback.")
|
| 83 |
-
else:
|
| 84 |
-
logger.info(f"Font file '{Config.FONT_PATH}' found.")
|
| 85 |
|
| 86 |
# --- Helper Functions ---
|
| 87 |
def add_noise_to_image(img: Image.Image, intensity: float = 0.02) -> Image.Image:
|
|
@@ -152,8 +149,8 @@ def apply_template(user_image_path: str, caption: str, template_path: str) -> Op
|
|
| 152 |
font_size = Config.MAX_FONT_SIZE // 2
|
| 153 |
logger.debug(f"Loading font '{Config.FONT_PATH}' with size {font_size}")
|
| 154 |
font = ImageFont.truetype(Config.FONT_PATH, font_size)
|
| 155 |
-
except IOError:
|
| 156 |
-
logger.warning(f"Failed to load font '{Config.FONT_PATH}'. Using Pillow's default.")
|
| 157 |
font = ImageFont.load_default()
|
| 158 |
|
| 159 |
# Wrap text according to configured width
|
|
@@ -278,8 +275,8 @@ def create_auto_template(user_image_path: str, caption: str, variant: int) -> Op
|
|
| 278 |
font_size = random.randint(Config.MIN_FONT_SIZE, Config.MAX_FONT_SIZE)
|
| 279 |
logger.debug(f"Loading font '{Config.FONT_PATH}' with size {font_size}")
|
| 280 |
font = ImageFont.truetype(Config.FONT_PATH, font_size)
|
| 281 |
-
except IOError:
|
| 282 |
-
logger.warning(f"Failed to load font '{Config.FONT_PATH}'. Using Pillow's default.")
|
| 283 |
font = ImageFont.load_default() # Fallback font
|
| 284 |
|
| 285 |
# Wrap text
|
|
|
|
| 33 |
# Directories (relative paths for container compatibility)
|
| 34 |
PREDEFINED_TEMPLATES_DIR = "templates"
|
| 35 |
OUTPUT_DIR = "generated_images"
|
| 36 |
+
# UPDATED: Use the font name "Arial". Pillow should find the system-installed font.
|
| 37 |
+
FONT_PATH = "Arial"
|
| 38 |
|
| 39 |
# Predefined Template Settings
|
| 40 |
TEMPLATE_SIZE = (1200, 900) # Expected size of predefined template canvas
|
|
|
|
| 76 |
# If directories cannot be created, the app likely cannot function
|
| 77 |
raise SystemExit(f"FATAL: Cannot create directories {Config.PREDEFINED_TEMPLATES_DIR} or {Config.OUTPUT_DIR}") from e
|
| 78 |
|
| 79 |
+
# Font check is now less critical as we rely on system fonts, but Pillow will log if "Arial" can't be found.
|
| 80 |
+
logger.info(f"Application will attempt to use font '{Config.FONT_PATH}' via system font discovery (fontconfig).")
|
| 81 |
+
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# --- Helper Functions ---
|
| 84 |
def add_noise_to_image(img: Image.Image, intensity: float = 0.02) -> Image.Image:
|
|
|
|
| 149 |
font_size = Config.MAX_FONT_SIZE // 2
|
| 150 |
logger.debug(f"Loading font '{Config.FONT_PATH}' with size {font_size}")
|
| 151 |
font = ImageFont.truetype(Config.FONT_PATH, font_size)
|
| 152 |
+
except IOError: # This can happen if font "Arial" is not found by Pillow
|
| 153 |
+
logger.warning(f"Failed to load font '{Config.FONT_PATH}' by name. Using Pillow's default. Ensure 'ttf-mscorefonts-installer' worked in Docker and fontconfig is effective.")
|
| 154 |
font = ImageFont.load_default()
|
| 155 |
|
| 156 |
# Wrap text according to configured width
|
|
|
|
| 275 |
font_size = random.randint(Config.MIN_FONT_SIZE, Config.MAX_FONT_SIZE)
|
| 276 |
logger.debug(f"Loading font '{Config.FONT_PATH}' with size {font_size}")
|
| 277 |
font = ImageFont.truetype(Config.FONT_PATH, font_size)
|
| 278 |
+
except IOError: # This can happen if font "Arial" is not found by Pillow
|
| 279 |
+
logger.warning(f"Failed to load font '{Config.FONT_PATH}' by name. Using Pillow's default. Ensure 'ttf-mscorefonts-installer' worked in Docker and fontconfig is effective.")
|
| 280 |
font = ImageFont.load_default() # Fallback font
|
| 281 |
|
| 282 |
# Wrap text
|