Spaces:
Running
on
Zero
Running
on
Zero
Update app-backup.py
Browse files- app-backup.py +112 -41
app-backup.py
CHANGED
@@ -191,57 +191,126 @@ def add_text_overlay(image, title_ko, author_ko,
|
|
191 |
# Get image dimensions
|
192 |
img_width, img_height = img_with_text.size
|
193 |
|
194 |
-
# Define position mappings
|
195 |
-
|
196 |
"μλ¨": (img_width // 2, img_height // 10),
|
197 |
"μ€μ": (img_width // 2, img_height // 2),
|
198 |
"νλ¨": (img_width // 2, img_height * 9 // 10)
|
199 |
}
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
# Draw title (Korean only)
|
202 |
if title_ko:
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
if author_ko:
|
223 |
# Add "μ§μμ΄: " prefix if not already present
|
224 |
if not author_ko.startswith("μ§μμ΄"):
|
225 |
author_text = f"μ§μμ΄: {author_ko}"
|
226 |
else:
|
227 |
author_text = author_ko
|
228 |
-
|
229 |
-
author_x, author_y = position_coords[author_position]
|
230 |
-
|
231 |
-
# Get text bbox for centering
|
232 |
-
try:
|
233 |
-
bbox = draw.textbbox((0, 0), author_text, font=author_font)
|
234 |
-
text_width = bbox[2] - bbox[0]
|
235 |
-
text_height = bbox[3] - bbox[1]
|
236 |
-
except:
|
237 |
-
# Fallback for older PIL versions
|
238 |
-
text_width, text_height = draw.textsize(author_text, font=author_font)
|
239 |
|
240 |
-
#
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
|
246 |
return img_with_text
|
247 |
|
@@ -383,8 +452,9 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
|
|
383 |
title_ko = gr.Textbox(label="μ λͺ©", placeholder="νκΈ μ λͺ©μ μ
λ ₯νμΈμ")
|
384 |
title_position = gr.Radio(
|
385 |
label="μ λͺ© μμΉ",
|
386 |
-
choices=["μλ¨", "μ€μ", "νλ¨"],
|
387 |
-
value="μλ¨"
|
|
|
388 |
)
|
389 |
title_size = gr.Slider(
|
390 |
label="μ λͺ© κΈμ ν¬κΈ°",
|
@@ -397,8 +467,9 @@ with gr.Blocks(theme=gr.themes.Soft(), analytics_enabled=False) as demo:
|
|
397 |
author_ko = gr.Textbox(label="μ§μμ΄", placeholder="μ§μμ΄ μ΄λ¦μ μ
λ ₯νμΈμ")
|
398 |
author_position = gr.Radio(
|
399 |
label="μ§μμ΄ μμΉ",
|
400 |
-
choices=["μλ¨", "μ€μ", "νλ¨"],
|
401 |
-
value="νλ¨"
|
|
|
402 |
)
|
403 |
author_size = gr.Slider(
|
404 |
label="μ§μμ΄ κΈμ ν¬κΈ°",
|
|
|
191 |
# Get image dimensions
|
192 |
img_width, img_height = img_with_text.size
|
193 |
|
194 |
+
# Define position mappings for horizontal text
|
195 |
+
horizontal_position_coords = {
|
196 |
"μλ¨": (img_width // 2, img_height // 10),
|
197 |
"μ€μ": (img_width // 2, img_height // 2),
|
198 |
"νλ¨": (img_width // 2, img_height * 9 // 10)
|
199 |
}
|
200 |
|
201 |
+
# Define position mappings for vertical text
|
202 |
+
vertical_position_coords = {
|
203 |
+
"μ’μΈ‘μλ¨": (img_width // 10, img_height // 10),
|
204 |
+
"μ€μμλ¨": (img_width // 2, img_height // 10),
|
205 |
+
"μ°μΈ‘μλ¨": (img_width * 9 // 10, img_height // 10)
|
206 |
+
}
|
207 |
+
|
208 |
+
shadow_offset = 2
|
209 |
+
|
210 |
# Draw title (Korean only)
|
211 |
if title_ko:
|
212 |
+
# Check if title position is vertical
|
213 |
+
if title_position in vertical_position_coords:
|
214 |
+
# Vertical text rendering
|
215 |
+
start_x, start_y = vertical_position_coords[title_position]
|
216 |
+
|
217 |
+
# For vertical text, we need to draw each character separately
|
218 |
+
for i, char in enumerate(title_ko):
|
219 |
+
# Get character bbox
|
220 |
+
try:
|
221 |
+
bbox = draw.textbbox((0, 0), char, font=title_font)
|
222 |
+
char_width = bbox[2] - bbox[0]
|
223 |
+
char_height = bbox[3] - bbox[1]
|
224 |
+
except:
|
225 |
+
# Fallback for older PIL versions
|
226 |
+
char_width, char_height = draw.textsize(char, font=title_font)
|
227 |
+
|
228 |
+
# Calculate position for this character
|
229 |
+
char_x = start_x - char_width // 2
|
230 |
+
char_y = start_y + i * (char_height + title_size // 10) # Add small spacing between characters
|
231 |
+
|
232 |
+
# Draw shadow
|
233 |
+
draw.text((char_x + shadow_offset, char_y + shadow_offset),
|
234 |
+
char, font=title_font, fill="black")
|
235 |
+
# Draw character
|
236 |
+
draw.text((char_x, char_y),
|
237 |
+
char, font=title_font, fill=text_color)
|
238 |
+
else:
|
239 |
+
# Horizontal text rendering (existing code)
|
240 |
+
title_x, title_y = horizontal_position_coords[title_position]
|
241 |
+
|
242 |
+
# Get text bbox for centering
|
243 |
+
try:
|
244 |
+
bbox = draw.textbbox((0, 0), title_ko, font=title_font)
|
245 |
+
text_width = bbox[2] - bbox[0]
|
246 |
+
text_height = bbox[3] - bbox[1]
|
247 |
+
except:
|
248 |
+
# Fallback for older PIL versions
|
249 |
+
text_width, text_height = draw.textsize(title_ko, font=title_font)
|
250 |
+
|
251 |
+
# Draw text with shadow for better visibility
|
252 |
+
draw.text((title_x - text_width // 2 + shadow_offset, title_y - text_height // 2 + shadow_offset),
|
253 |
+
title_ko, font=title_font, fill="black")
|
254 |
+
draw.text((title_x - text_width // 2, title_y - text_height // 2),
|
255 |
+
title_ko, font=title_font, fill=text_color)
|
256 |
+
|
257 |
+
# Draw author (Korean only) - keeping horizontal only for now
|
258 |
if author_ko:
|
259 |
# Add "μ§μμ΄: " prefix if not already present
|
260 |
if not author_ko.startswith("μ§μμ΄"):
|
261 |
author_text = f"μ§μμ΄: {author_ko}"
|
262 |
else:
|
263 |
author_text = author_ko
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
+
# Check if author position is vertical
|
266 |
+
if author_position in vertical_position_coords:
|
267 |
+
# Vertical text rendering for author
|
268 |
+
start_x, start_y = vertical_position_coords[author_position]
|
269 |
+
|
270 |
+
# Adjust start_y for author (place it lower than title if both are vertical)
|
271 |
+
if title_position in vertical_position_coords:
|
272 |
+
# Calculate how much space the title takes
|
273 |
+
title_length = len(title_ko) if title_ko else 0
|
274 |
+
start_y += title_length * (title_size + title_size // 10) + author_size
|
275 |
+
|
276 |
+
for i, char in enumerate(author_text):
|
277 |
+
# Get character bbox
|
278 |
+
try:
|
279 |
+
bbox = draw.textbbox((0, 0), char, font=author_font)
|
280 |
+
char_width = bbox[2] - bbox[0]
|
281 |
+
char_height = bbox[3] - bbox[1]
|
282 |
+
except:
|
283 |
+
# Fallback for older PIL versions
|
284 |
+
char_width, char_height = draw.textsize(char, font=author_font)
|
285 |
+
|
286 |
+
# Calculate position for this character
|
287 |
+
char_x = start_x - char_width // 2
|
288 |
+
char_y = start_y + i * (char_height + author_size // 10)
|
289 |
+
|
290 |
+
# Draw shadow
|
291 |
+
draw.text((char_x + shadow_offset, char_y + shadow_offset),
|
292 |
+
char, font=author_font, fill="black")
|
293 |
+
# Draw character
|
294 |
+
draw.text((char_x, char_y),
|
295 |
+
char, font=author_font, fill=text_color)
|
296 |
+
else:
|
297 |
+
# Horizontal text rendering (existing code)
|
298 |
+
author_x, author_y = horizontal_position_coords[author_position]
|
299 |
+
|
300 |
+
# Get text bbox for centering
|
301 |
+
try:
|
302 |
+
bbox = draw.textbbox((0, 0), author_text, font=author_font)
|
303 |
+
text_width = bbox[2] - bbox[0]
|
304 |
+
text_height = bbox[3] - bbox[1]
|
305 |
+
except:
|
306 |
+
# Fallback for older PIL versions
|
307 |
+
text_width, text_height = draw.textsize(author_text, font=author_font)
|
308 |
+
|
309 |
+
# Draw text with shadow
|
310 |
+
draw.text((author_x - text_width // 2 + shadow_offset, author_y - text_height // 2 + shadow_offset),
|
311 |
+
author_text, font=author_font, fill="black")
|
312 |
+
draw.text((author_x - text_width // 2, author_y - text_height // 2),
|
313 |
+
author_text, font=author_font, fill=text_color)
|
314 |
|
315 |
return img_with_text
|
316 |
|
|
|
452 |
title_ko = gr.Textbox(label="μ λͺ©", placeholder="νκΈ μ λͺ©μ μ
λ ₯νμΈμ")
|
453 |
title_position = gr.Radio(
|
454 |
label="μ λͺ© μμΉ",
|
455 |
+
choices=["μλ¨", "μ€μ", "νλ¨", "μ’μΈ‘μλ¨", "μ€μμλ¨", "μ°μΈ‘μλ¨"],
|
456 |
+
value="μλ¨",
|
457 |
+
info="μ’μΈ‘μλ¨, μ€μμλ¨, μ°μΈ‘μλ¨μ μΈλ‘μ°κΈ°μ
λλ€"
|
458 |
)
|
459 |
title_size = gr.Slider(
|
460 |
label="μ λͺ© κΈμ ν¬κΈ°",
|
|
|
467 |
author_ko = gr.Textbox(label="μ§μμ΄", placeholder="μ§μμ΄ μ΄λ¦μ μ
λ ₯νμΈμ")
|
468 |
author_position = gr.Radio(
|
469 |
label="μ§μμ΄ μμΉ",
|
470 |
+
choices=["μλ¨", "μ€μ", "νλ¨", "μ’μΈ‘μλ¨", "μ€μμλ¨", "μ°μΈ‘μλ¨"],
|
471 |
+
value="νλ¨",
|
472 |
+
info="μ’μΈ‘μλ¨, μ€μμλ¨, μ°μΈ‘μλ¨μ μΈλ‘μ°κΈ°μ
λλ€"
|
473 |
)
|
474 |
author_size = gr.Slider(
|
475 |
label="μ§μμ΄ κΈμ ν¬κΈ°",
|