Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
from tqdm import tqdm
|
|
|
6 |
import time
|
7 |
|
8 |
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
@@ -42,9 +43,32 @@ def infer(color_prompt, phone_type_prompt, design_prompt):
|
|
42 |
else:
|
43 |
raise Exception(f"API Error: {response.status_code}")
|
44 |
|
45 |
-
# Function to save the design
|
46 |
-
def save_design(image):
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
image.save(file_path)
|
49 |
return f"Design saved as {file_path}!"
|
50 |
|
@@ -142,7 +166,7 @@ with gr.Blocks(css=custom_css) as interface:
|
|
142 |
)
|
143 |
save_button.click(
|
144 |
save_design,
|
145 |
-
inputs=[output_image],
|
146 |
outputs=output_message,
|
147 |
)
|
148 |
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
from io import BytesIO
|
5 |
from tqdm import tqdm
|
6 |
+
import numpy as np
|
7 |
import time
|
8 |
|
9 |
repo = "artificialguybr/TshirtDesignRedmond-V2"
|
|
|
43 |
else:
|
44 |
raise Exception(f"API Error: {response.status_code}")
|
45 |
|
46 |
+
# Function to save the design with a caption
|
47 |
+
def save_design(image, color_prompt, phone_type_prompt, design_prompt):
|
48 |
+
# Convert NumPy array to PIL Image
|
49 |
+
if isinstance(image, np.ndarray):
|
50 |
+
image = Image.fromarray(image)
|
51 |
+
|
52 |
+
# Add caption
|
53 |
+
draw = ImageDraw.Draw(image)
|
54 |
+
caption = f"Color: {color_prompt}\nMobile Type: {phone_type_prompt}\nDesign: {design_prompt}"
|
55 |
+
font_size = 20
|
56 |
+
|
57 |
+
try:
|
58 |
+
# Attempt to load a default font
|
59 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
60 |
+
except IOError:
|
61 |
+
font = ImageFont.load_default()
|
62 |
+
|
63 |
+
# Calculate position for text
|
64 |
+
text_width, text_height = draw.multiline_textsize(caption, font=font)
|
65 |
+
position = (10, image.height - text_height - 10) # Bottom-left corner with padding
|
66 |
+
|
67 |
+
# Add caption to the image
|
68 |
+
draw.multiline_text(position, caption, fill="white", font=font)
|
69 |
+
|
70 |
+
# Save the image
|
71 |
+
file_path = "saved_design_with_caption.png"
|
72 |
image.save(file_path)
|
73 |
return f"Design saved as {file_path}!"
|
74 |
|
|
|
166 |
)
|
167 |
save_button.click(
|
168 |
save_design,
|
169 |
+
inputs=[output_image, color_prompt, phone_type_prompt, design_prompt],
|
170 |
outputs=output_message,
|
171 |
)
|
172 |
|