aiqtech commited on
Commit
6bc4578
·
verified ·
1 Parent(s): f0c49d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -65
app.py CHANGED
@@ -529,77 +529,84 @@ def add_text_to_image(
529
  """
530
  Add text to an image with customizable properties
531
  """
532
- # Convert gradio image (numpy array) to PIL Image
533
- if input_image is None:
534
- return None
535
-
536
- image = Image.fromarray(input_image)
537
- # remove background
538
- overlay_image = remove_background(image)
539
-
540
- # Create a transparent overlay for the text
541
- txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
542
- draw = ImageDraw.Draw(txt_overlay)
543
-
544
- # Create a font with specified size
545
  try:
546
- font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
547
- except:
548
- # If DejaVu font is not found, try to use Arial or default
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549
  try:
550
- font = ImageFont.truetype("arial.ttf", int(font_size))
551
  except:
552
- print("Using default font as system fonts not found")
553
- font = ImageFont.load_default()
554
-
555
- # Convert color name to RGB
556
- color_map = {
557
- 'White': (255, 255, 255),
558
- 'Black': (0, 0, 0),
559
- 'Red': (255, 0, 0),
560
- 'Green': (0, 255, 0),
561
- 'Blue': (0, 0, 255),
562
- 'Yellow': (255, 255, 0),
563
- 'Purple': (128, 0, 128)
564
- }
565
- rgb_color = color_map.get(color, (255, 255, 255))
566
-
567
- # Get text size for positioning
568
- text_bbox = draw.textbbox((0, 0), text, font=font)
569
- text_width = text_bbox[2] - text_bbox[0]
570
- text_height = text_bbox[3] - text_bbox[1]
571
-
572
- # Calculate actual x and y positions based on percentages
573
- actual_x = int((image.width - text_width) * (x_position / 100))
574
- actual_y = int((image.height - text_height) * (y_position / 100))
575
 
576
- # Create final color with opacity
577
- text_color = (*rgb_color, int(opacity))
578
-
579
- # Draw the text with stroke for thickness
580
- add_text_with_stroke(
581
- draw,
582
- text,
583
- actual_x,
584
- actual_y,
585
- font,
586
- text_color,
587
- int(thickness)
588
- )
589
 
590
- # Combine the original image with the text overlay
591
- if image.mode != 'RGBA':
592
- image = image.convert('RGBA')
593
- output_image = Image.alpha_composite(image, txt_overlay)
594
-
595
- # Convert back to RGB for display
596
- output_image = output_image.convert('RGB')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
 
598
- # superimpose images
599
- output_image = superimpose(output_image, overlay_image)
600
-
601
- # Convert PIL image back to numpy array for Gradio
602
- return np.array(output_image)
603
 
604
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
605
  gr.HTML("""
 
529
  """
530
  Add text to an image with customizable properties
531
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
532
  try:
533
+ # 입력 이미지 처리 수정
534
+ if input_image is None:
535
+ return None
536
+
537
+ # PIL Image 객체로 변환 (이미 PIL Image인 경우 그대로 사용)
538
+ if not isinstance(input_image, Image.Image):
539
+ if isinstance(input_image, np.ndarray):
540
+ image = Image.fromarray(input_image)
541
+ else:
542
+ raise ValueError("Unsupported image type")
543
+ else:
544
+ image = input_image.copy()
545
+
546
+ # 이미지를 RGBA 모드로 변환
547
+ if image.mode != 'RGBA':
548
+ image = image.convert('RGBA')
549
+
550
+ # Create a transparent overlay for the text
551
+ txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
552
+ draw = ImageDraw.Draw(txt_overlay)
553
+
554
+ # Create a font with specified size
555
  try:
556
+ font = ImageFont.truetype("DejaVuSans.ttf", int(font_size))
557
  except:
558
+ try:
559
+ font = ImageFont.truetype("arial.ttf", int(font_size))
560
+ except:
561
+ print("Using default font")
562
+ font = ImageFont.load_default()
563
+
564
+ # Convert color name to RGB
565
+ color_map = {
566
+ 'White': (255, 255, 255),
567
+ 'Black': (0, 0, 0),
568
+ 'Red': (255, 0, 0),
569
+ 'Green': (0, 255, 0),
570
+ 'Blue': (0, 0, 255),
571
+ 'Yellow': (255, 255, 0),
572
+ 'Purple': (128, 0, 128)
573
+ }
574
+ rgb_color = color_map.get(color, (255, 255, 255))
575
+
576
+ # Get text size for positioning
577
+ text_bbox = draw.textbbox((0, 0), text, font=font)
578
+ text_width = text_bbox[2] - text_bbox[0]
579
+ text_height = text_bbox[3] - text_bbox[1]
 
580
 
581
+ # Calculate actual positions
582
+ actual_x = int((image.width - text_width) * (x_position / 100))
583
+ actual_y = int((image.height - text_height) * (y_position / 100))
 
 
 
 
 
 
 
 
 
 
584
 
585
+ # Create final color with opacity
586
+ text_color = (*rgb_color, int(opacity))
587
+
588
+ # Draw text with stroke
589
+ add_text_with_stroke(
590
+ draw,
591
+ text,
592
+ actual_x,
593
+ actual_y,
594
+ font,
595
+ text_color,
596
+ int(thickness)
597
+ )
598
+
599
+ # Combine the original image with the text overlay
600
+ output_image = Image.alpha_composite(image, txt_overlay)
601
+
602
+ # Convert back to RGB for display
603
+ output_image = output_image.convert('RGB')
604
+
605
+ return output_image
606
 
607
+ except Exception as e:
608
+ print(f"Error in add_text_to_image: {str(e)}")
609
+ return input_image # 에러 발생 시 원본 이미지 반환
 
 
610
 
611
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
612
  gr.HTML("""