File size: 8,461 Bytes
3f4f2ec
 
ed46214
3cc8b5d
3f4f2ec
a00258b
3cc8b5d
3f4f2ec
 
 
 
3cc8b5d
ed46214
3cc8b5d
 
 
 
3f4f2ec
3cc8b5d
1f8d09e
d99630a
3cc8b5d
 
1f8d09e
3cc8b5d
d99630a
3cc8b5d
d99630a
3cc8b5d
 
 
d99630a
3cc8b5d
 
a00258b
 
 
 
 
 
 
 
3cc8b5d
 
a00258b
3cc8b5d
 
 
 
 
 
 
 
 
 
 
 
d99630a
3cc8b5d
 
 
3f4f2ec
 
3cc8b5d
 
 
 
 
 
 
 
 
 
 
3f4f2ec
a00258b
3cc8b5d
9d92c0c
 
 
5a992e2
3cc8b5d
ed46214
3cc8b5d
 
 
 
ed46214
3cc8b5d
9d92c0c
d0e1b15
dddc7cb
3cc8b5d
9d92c0c
d0e1b15
9d92c0c
1f8d09e
d99630a
3cc8b5d
1f8d09e
3cc8b5d
d99630a
 
3cc8b5d
 
 
d99630a
3cc8b5d
d99630a
a00258b
 
 
 
 
 
 
 
3cc8b5d
1f8d09e
3cc8b5d
 
 
 
 
 
 
 
 
 
9d92c0c
a00258b
9d92c0c
3cc8b5d
 
 
9d92c0c
5a992e2
3cc8b5d
9d92c0c
 
606b3d4
 
500a532
 
d0e1b15
500a532
606b3d4
 
 
9d92c0c
469cd38
3cc8b5d
606b3d4
469cd38
9d92c0c
 
3cc8b5d
9d92c0c
 
 
 
3f4f2ec
 
3cc8b5d
 
3f4f2ec
 
 
 
 
 
 
 
 
 
 
469cd38
 
 
53ff9a3
d6bf45b
d99630a
469cd38
 
 
a00258b
3cc8b5d
469cd38
 
 
 
d99630a
1f8d09e
469cd38
 
 
3f4f2ec
bbfe5e6
 
3f4f2ec
 
 
 
1f8d09e
3f4f2ec
 
3cc8b5d
 
3f4f2ec
 
 
 
 
 
 
 
 
 
 
 
bbfe5e6
 
3f4f2ec
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from PIL import Image, ImageDraw, ImageFont
import gradio as gr
import numpy as np
import cv2

def add_text_to_image(image, text, text_x, text_y, font_size="80px", font_family="Standard", font_color="Weiß"):
    """Fügt Text mit OpenCV zum Bild hinzu - garantiert skalierbar!"""
    if image is None or not text:
        return image
        
    try:
        # Konvertiere PIL Image zu OpenCV Format
        if isinstance(image, np.ndarray):
            opencv_image = image
        else:
            opencv_image = np.array(image)
            opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_RGB2BGR)
        
        # Schriftgröße
        try:
            pixel_size = int(font_size.replace("px", ""))
            # OpenCV Font-Skala anpassen (empirische Werte)
            font_scale = pixel_size / 30.0
        except:
            font_scale = 2.0
        
        # Schriftfarbe
        color_mapping = {
            "Weiß": (255, 255, 255),
            "Schwarz": (0, 0, 0),
            "Rot": (0, 0, 255)
        }
        actual_color = color_mapping.get(font_color, (255, 255, 255))
        
        # OpenCV Font basierend auf Auswahl
        font_mapping = {
            "Standard": cv2.FONT_HERSHEY_SIMPLEX,
            "Einfache Handschrift": cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
            "Verschnörkelte Handschrift": cv2.FONT_HERSHEY_SCRIPT_COMPLEX
        }
        
        font = font_mapping.get(font_family, cv2.FONT_HERSHEY_SIMPLEX)
        thickness = max(2, int(pixel_size / 20))
        
        print(f"OpenCV Text: '{text}' an ({text_x}, {text_y}) - Schrift: {font_family}, Größe: {font_scale:.2f}, Dicke: {thickness}")
        
        # Text mit OpenCV hinzufügen
        cv2.putText(
            opencv_image, 
            text, 
            (int(text_x), int(text_y)), 
            font, 
            font_scale, 
            actual_color, 
            thickness, 
            cv2.LINE_AA
        )
        
        # Zurück zu PIL Format
        result_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
        return Image.fromarray(result_image)
        
    except Exception as e:
        print(f"Fehler beim Text hinzufügen mit OpenCV: {e}")
        # Fallback zu PIL
        try:
            if isinstance(image, np.ndarray):
                image = Image.fromarray(image)
            image_with_text = image.copy()
            draw = ImageDraw.Draw(image_with_text)
            draw.text((text_x, text_y), text, fill="white")
            return image_with_text
        except:
            return image

def create_text_preview(image, text, text_x, text_y, font_size="80px", font_family="Standard", font_color="Weiß"):
    """Vorschau mit OpenCV"""
    if image is None:
        return image
        
    try:
        # Zu OpenCV konvertieren
        if isinstance(image, np.ndarray):
            opencv_image = image
        else:
            opencv_image = np.array(image)
            opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_RGB2BGR)
        
        preview = opencv_image.copy()
        
        # Rote Marker
        if text and text_x is not None and text_y is not None and (text_x != 100 or text_y != 100):
            cv2.circle(preview, (int(text_x), int(text_y)), 8, (0, 0, 255), -1)
        
        # Text zeichnen
        if text:
            try:
                pixel_size = int(font_size.replace("px", ""))
                font_scale = pixel_size / 30.0
            except:
                font_scale = 2.0
            
            color_mapping = {
                "Weiß": (255, 255, 255),
                "Schwarz": (0, 0, 0),
                "Rot": (0, 0, 255)
            }
            actual_color = color_mapping.get(font_color, (255, 255, 255))
            
            # OpenCV Font basierend auf Auswahl
            font_mapping = {
                "Standard": cv2.FONT_HERSHEY_SIMPLEX,
                "Einfache Handschrift": cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
                "Verschnörkelte Handschrift": cv2.FONT_HERSHEY_SCRIPT_COMPLEX
            }
            
            font = font_mapping.get(font_family, cv2.FONT_HERSHEY_SIMPLEX)
            thickness = max(2, int(pixel_size / 20))
            
            cv2.putText(
                preview, 
                text, 
                (int(text_x), int(text_y)), 
                font, 
                font_scale, 
                actual_color, 
                thickness, 
                cv2.LINE_AA
            )
            
            print(f"OpenCV Vorschau: Schrift {font_family}, Größe {font_scale:.2f}")
        
        # Zurück zu PIL
        result_image = cv2.cvtColor(preview, cv2.COLOR_BGR2RGB)
        return Image.fromarray(result_image)
        
    except Exception as e:
        print(f"Fehler bei OpenCV Vorschau: {e}")
        return image

def update_text_preview_i2i(original_image, generated_image, text, text_x, text_y, font_size, font_family, font_color, target_selector):
    """Aktualisiert die Text-Vorschau für Bild-zu-Bild mit Formatierung"""
    if target_selector == "Originalbild":
        target_image = original_image
    else:
        target_image = generated_image
    
    print(f"Update Vorschau I2I - Größe: {font_size}, Schrift: {font_family}, Farbe: {font_color}, Ziel: {target_selector}")
    return create_text_preview(target_image, text, text_x, text_y, font_size, font_family, font_color)

def update_text_preview_t2i(image, text, text_x, text_y, font_size, font_family, font_color):
    """Aktualisiert die Text-Vorschau für Text-zu-Bild mit Formatierung"""
    print(f"Update Vorschau T2I - Größe: {font_size}, Schrift: {font_family}, Farbe: {font_color}")
    return create_text_preview(image, text, text_x, text_y, font_size, font_family, font_color)

def capture_click(event: gr.SelectData):
    """Handhabt Klicks in beiden Tabs"""
    if event and hasattr(event, 'index'):
        print(f"🎯 CLICK ERFOLGREICH: ({event.index[0]}, {event.index[1]})")
        return event.index[0], event.index[1]
    return 100, 100

def create_text_integration_section_t2i():
    """Erstellt die UI-Komponenten für Text-Integration in Text-zu-Bild"""
    
    with gr.Row():
        gr.Markdown("### 📝 Text auf Bild integrieren")
    
    with gr.Row():
        text_input_t2i = gr.Textbox(
            label="Text eingeben",
            placeholder="Dein Text hier...",
            max_lines=2,
            scale=3
        )
        
    with gr.Row():
        with gr.Column():
            font_size_t2i = gr.Dropdown(
                choices=["120px", "80px", "40px"],
                value="80px",
                label="Schriftgröße"
            )
        with gr.Column():
            font_family_t2i = gr.Dropdown(
                choices=["Standard", "Einfache Handschrift", "Verschnörkelte Handschrift"],
                value="Standard", 
                label="Schriftart"
            )
        with gr.Column():
            font_color_t2i = gr.Dropdown(
                choices=["Weiß", "Schwarz", "Rot"],
                value="Weiß",
                label="Schriftfarbe"
            )
        
    with gr.Row():
        text_x_t2i = gr.Number(value=None, visible=False)
        text_y_t2i = gr.Number(value=None, visible=False)
        
    with gr.Row():
        text_btn_t2i = gr.Button("📝 Text auf generiertes Bild", variant="secondary")
    
    return text_input_t2i, text_x_t2i, text_y_t2i, font_size_t2i, font_family_t2i, font_color_t2i, text_btn_t2i

def create_text_integration_section_i2i():
    """Erstellt die UI-Komponenten für Text-Integration in Bild-zu-Bild"""
    
    with gr.Row():
        gr.Markdown("### 📝 Text auf Bild integrieren")
    
    with gr.Row():
        text_input_i2i = gr.Textbox(
            label="Text eingeben",
            placeholder="Dein Text hier...",
            max_lines=2,
            scale=3
        )
        
    with gr.Row():
        text_x_i2i = gr.Number(value=None, visible=False)
        text_y_i2i = gr.Number(value=None, visible=False)
        
    with gr.Row():
        target_selector = gr.Radio(
            choices=["Originalbild", "Generiertes Bild"],
            value="Generiertes Bild",
            label="Text auf welchem Bild?",
            scale=2
        )
        
    with gr.Row():
        text_btn_i2i = gr.Button("📝 Text integrieren", variant="secondary")
    
    return text_input_i2i, text_x_i2i, text_y_i2i, target_selector, text_btn_i2i