Astridkraft commited on
Commit
1f8d09e
·
verified ·
1 Parent(s): b1ec392

Update text_integration.py

Browse files
Files changed (1) hide show
  1. text_integration.py +52 -14
text_integration.py CHANGED
@@ -2,7 +2,7 @@ from PIL import Image, ImageDraw, ImageFont
2
  import gradio as gr
3
  import numpy as np
4
 
5
- def add_text_to_image(image, text, text_x, text_y, font_size=90, font_family="Arial", font_color="#FFFFFF"):
6
  """Fügt Text mit PIL zum Bild hinzu mit Formatierungsoptionen"""
7
  if image is None or not text:
8
  return image
@@ -15,6 +15,19 @@ def add_text_to_image(image, text, text_x, text_y, font_size=90, font_family="Ar
15
  image_with_text = image.copy()
16
  draw = ImageDraw.Draw(image_with_text)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # Schriftart basierend auf Auswahl
19
  font_paths = {
20
  "Arial": "arial.ttf",
@@ -24,20 +37,26 @@ def add_text_to_image(image, text, text_x, text_y, font_size=90, font_family="Ar
24
 
25
  try:
26
  font_path = font_paths.get(font_family, "arial.ttf")
27
- font = ImageFont.truetype(font_path, int(font_size))
28
- except:
 
 
29
  # Fallback auf Default Font
30
- font = ImageFont.load_default()
 
 
 
 
31
 
32
- draw.text((text_x, text_y), text, fill=font_color, font=font)
33
- print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt - Größe: {font_size}, Schrift: {font_family}, Farbe: {font_color}")
34
  return image_with_text
35
 
36
  except Exception as e:
37
  print(f"Fehler beim Text hinzufügen: {e}")
38
  return image
39
 
40
- def create_text_preview(image, text, text_x, text_y, font_size=90, font_family="Arial", font_color="#FFFFFF"):
41
  """Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
42
  if image is None:
43
  return image
@@ -65,6 +84,19 @@ def create_text_preview(image, text, text_x, text_y, font_size=90, font_family="
65
 
66
  # Text zeichnen wenn vorhanden
67
  if text:
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  # Schriftart basierend auf Auswahl
69
  font_paths = {
70
  "Arial": "arial.ttf",
@@ -74,12 +106,17 @@ def create_text_preview(image, text, text_x, text_y, font_size=90, font_family="
74
 
75
  try:
76
  font_path = font_paths.get(font_family, "arial.ttf")
77
- font = ImageFont.truetype(font_path, int(font_size))
78
- except:
 
79
  # Fallback auf Default Font
80
- font = ImageFont.load_default()
 
 
 
81
 
82
- draw.text((text_x, text_y), text, fill=font_color, font=font)
 
83
 
84
  return preview
85
 
@@ -98,6 +135,7 @@ def update_text_preview_i2i(original_image, generated_image, text, text_x, text_
98
 
99
  def update_text_preview_t2i(image, text, text_x, text_y, font_size, font_family, font_color):
100
  """Aktualisiert die Text-Vorschau für Text-zu-Bild mit Formatierung"""
 
101
  return create_text_preview(image, text, text_x, text_y, font_size, font_family, font_color)
102
 
103
  def capture_click(event: gr.SelectData):
@@ -138,8 +176,8 @@ def create_text_integration_section_t2i():
138
  )
139
  with gr.Column():
140
  font_color_t2i = gr.Dropdown(
141
- choices=["#FFFFFF", "#000000"],
142
- value="#FFFFFF",
143
  label="Schriftfarbe"
144
  )
145
 
@@ -151,7 +189,7 @@ def create_text_integration_section_t2i():
151
  with gr.Row():
152
  text_btn_t2i = gr.Button("📝 Text auf generiertes Bild", variant="secondary")
153
 
154
- return text_input_t2i, text_x_t2i, text_y_t2i, text_btn_t2i
155
 
156
  def create_text_integration_section_i2i():
157
  """Erstellt die UI-Komponenten für Text-Integration in Bild-zu-Bild"""
 
2
  import gradio as gr
3
  import numpy as np
4
 
5
+ def add_text_to_image(image, text, text_x, text_y, font_size="90", font_family="Arial", font_color="Weiß"):
6
  """Fügt Text mit PIL zum Bild hinzu mit Formatierungsoptionen"""
7
  if image is None or not text:
8
  return image
 
15
  image_with_text = image.copy()
16
  draw = ImageDraw.Draw(image_with_text)
17
 
18
+ # Schriftgröße direkt verwenden (kein Mapping mehr)
19
+ try:
20
+ pixel_size = int(font_size)
21
+ except:
22
+ pixel_size = 90
23
+
24
+ # Farbnamen in Hex-Codes umwandeln
25
+ color_mapping = {
26
+ "Weiß": "#FFFFFF",
27
+ "Schwarz": "#000000"
28
+ }
29
+ hex_color = color_mapping.get(font_color, "#FFFFFF")
30
+
31
  # Schriftart basierend auf Auswahl
32
  font_paths = {
33
  "Arial": "arial.ttf",
 
37
 
38
  try:
39
  font_path = font_paths.get(font_family, "arial.ttf")
40
+ font = ImageFont.truetype(font_path, pixel_size)
41
+ print(f"Schriftart geladen: {font_path} mit Größe {pixel_size}px")
42
+ except Exception as font_error:
43
+ print(f"Fehler beim Laden der Schriftart: {font_error}")
44
  # Fallback auf Default Font
45
+ try:
46
+ font = ImageFont.truetype("arial.ttf", pixel_size)
47
+ except:
48
+ font = ImageFont.load_default()
49
+ print("Fallback auf Default Font")
50
 
51
+ draw.text((text_x, text_y), text, fill=hex_color, font=font)
52
+ print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt - Größe: {pixel_size}px, Schrift: {font_family}, Farbe: {font_color} ({hex_color})")
53
  return image_with_text
54
 
55
  except Exception as e:
56
  print(f"Fehler beim Text hinzufügen: {e}")
57
  return image
58
 
59
+ def create_text_preview(image, text, text_x, text_y, font_size="90", font_family="Arial", font_color="Weiß"):
60
  """Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
61
  if image is None:
62
  return image
 
84
 
85
  # Text zeichnen wenn vorhanden
86
  if text:
87
+ # Schriftgröße direkt verwenden (kein Mapping mehr)
88
+ try:
89
+ pixel_size = int(font_size)
90
+ except:
91
+ pixel_size = 90
92
+
93
+ # Farbnamen in Hex-Codes umwandeln
94
+ color_mapping = {
95
+ "Weiß": "#FFFFFF",
96
+ "Schwarz": "#000000"
97
+ }
98
+ hex_color = color_mapping.get(font_color, "#FFFFFF")
99
+
100
  # Schriftart basierend auf Auswahl
101
  font_paths = {
102
  "Arial": "arial.ttf",
 
106
 
107
  try:
108
  font_path = font_paths.get(font_family, "arial.ttf")
109
+ font = ImageFont.truetype(font_path, pixel_size)
110
+ except Exception as font_error:
111
+ print(f"Fehler beim Laden der Schriftart für Vorschau: {font_error}")
112
  # Fallback auf Default Font
113
+ try:
114
+ font = ImageFont.truetype("arial.ttf", pixel_size)
115
+ except:
116
+ font = ImageFont.load_default()
117
 
118
+ draw.text((text_x, text_y), text, fill=hex_color, font=font)
119
+ print(f"Vorschau: Text mit Größe {pixel_size}px, Farbe {font_color} ({hex_color}) gezeichnet")
120
 
121
  return preview
122
 
 
135
 
136
  def update_text_preview_t2i(image, text, text_x, text_y, font_size, font_family, font_color):
137
  """Aktualisiert die Text-Vorschau für Text-zu-Bild mit Formatierung"""
138
+ print(f"Update Vorschau - Größe: {font_size}px, Schrift: {font_family}, Farbe: {font_color}")
139
  return create_text_preview(image, text, text_x, text_y, font_size, font_family, font_color)
140
 
141
  def capture_click(event: gr.SelectData):
 
176
  )
177
  with gr.Column():
178
  font_color_t2i = gr.Dropdown(
179
+ choices=["Weiß", "Schwarz"],
180
+ value="Weiß",
181
  label="Schriftfarbe"
182
  )
183
 
 
189
  with gr.Row():
190
  text_btn_t2i = gr.Button("📝 Text auf generiertes Bild", variant="secondary")
191
 
192
+ return text_input_t2i, text_x_t2i, text_y_t2i, font_size_t2i, font_family_t2i, font_color_t2i, text_btn_t2i
193
 
194
  def create_text_integration_section_i2i():
195
  """Erstellt die UI-Komponenten für Text-Integration in Bild-zu-Bild"""