ginipick commited on
Commit
0c364a7
Β·
verified Β·
1 Parent(s): fe2ec25

Update app-backup.py

Browse files
Files changed (1) hide show
  1. 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
- 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
  # Draw title (Korean only)
202
  if title_ko:
203
- title_x, title_y = position_coords[title_position]
204
-
205
- # Get text bbox for centering
206
- try:
207
- bbox = draw.textbbox((0, 0), title_ko, font=title_font)
208
- text_width = bbox[2] - bbox[0]
209
- text_height = bbox[3] - bbox[1]
210
- except:
211
- # Fallback for older PIL versions
212
- text_width, text_height = draw.textsize(title_ko, font=title_font)
213
-
214
- # Draw text with shadow for better visibility
215
- shadow_offset = 2
216
- draw.text((title_x - text_width // 2 + shadow_offset, title_y - text_height // 2 + shadow_offset),
217
- title_ko, font=title_font, fill="black")
218
- draw.text((title_x - text_width // 2, title_y - text_height // 2),
219
- title_ko, font=title_font, fill=text_color)
220
-
221
- # Draw author (Korean only)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Draw text with shadow
241
- draw.text((author_x - text_width // 2 + shadow_offset, author_y - text_height // 2 + shadow_offset),
242
- author_text, font=author_font, fill="black")
243
- draw.text((author_x - text_width // 2, author_y - text_height // 2),
244
- author_text, font=author_font, fill=text_color)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="지은이 κΈ€μž 크기",