Mikiko Bazeley
commited on
Commit
·
c6c35c1
1
Parent(s):
158efc9
Added more options for postcard customization
Browse files
pages/1_Generate_Holiday_Postcard.py
CHANGED
@@ -5,6 +5,8 @@ from PIL import Image, ImageDraw, ImageFont
|
|
5 |
from io import BytesIO
|
6 |
import streamlit as st
|
7 |
import textwrap
|
|
|
|
|
8 |
|
9 |
# Set page configuration
|
10 |
st.set_page_config(page_title="FLUX Image Generation Tool", page_icon="🎇")
|
@@ -55,6 +57,29 @@ holiday_prompts = [
|
|
55 |
"A lively Independence Day celebration with colorful fireworks lighting up the night sky, flags waving in the breeze, and a peaceful lakeside reflecting the celebration"
|
56 |
]
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
60 |
# Function to make requests to the FLUX models
|
@@ -90,13 +115,23 @@ def wrap_text(text, max_chars):
|
|
90 |
return "\n".join(textwrap.fill(line, width=max_chars) for line in text.split("\n"))
|
91 |
|
92 |
# Function to add the holiday message to the image
|
93 |
-
|
|
|
|
|
|
|
94 |
try:
|
95 |
font = ImageFont.truetype(font_path, font_size)
|
96 |
except IOError:
|
97 |
font = ImageFont.load_default()
|
98 |
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
# Wrap the text based on the max_chars input
|
102 |
message = wrap_text(message, max_chars)
|
@@ -105,7 +140,7 @@ def add_custom_message(image, message, font_path, font_size, position_vertical,
|
|
105 |
|
106 |
# Calculate the text bounding box dynamically
|
107 |
text_lines = message.split("\n")
|
108 |
-
line_height = draw.textbbox((0, 0), "A", font=font)[3] # Get height of one line of text
|
109 |
total_text_height = line_height * len(text_lines)
|
110 |
text_width = max([draw.textbbox((0, 0), line, font=font)[2] for line in text_lines])
|
111 |
|
@@ -128,14 +163,23 @@ def add_custom_message(image, message, font_path, font_size, position_vertical,
|
|
128 |
# Define the padding for the background box behind the text
|
129 |
padding = 10
|
130 |
|
131 |
-
#
|
132 |
-
|
|
|
|
|
|
|
133 |
|
134 |
# Draw the text line by line with the user-specified font color
|
135 |
for i, line in enumerate(text_lines):
|
136 |
draw.text((x_pos, y_pos + i * line_height), line, font=font, fill=font_color)
|
137 |
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
# Streamlit UI
|
141 |
st.title("FLUX Image Generation")
|
@@ -147,10 +191,13 @@ custom_prompt = st.text_input("Enter your custom prompt") if selected_prompt ==
|
|
147 |
prompt = custom_prompt if selected_prompt == "Custom" else selected_prompt
|
148 |
|
149 |
# Dropdown to select the model
|
150 |
-
model_choice = st.selectbox("Select the model:", ["flux-1-
|
|
|
|
|
|
|
|
|
151 |
|
152 |
# Additional inputs for customizing the message
|
153 |
-
message = st.text_input("Enter a holiday message to add:")
|
154 |
font_choice = st.selectbox("Select a font:", list(fonts.keys()))
|
155 |
font_size = st.slider("Select font size:", 10, 100, 40)
|
156 |
max_chars = st.slider("Max characters per line:", 10, 100, 40) # Slider to select character wrap limit
|
@@ -159,10 +206,14 @@ max_chars = st.slider("Max characters per line:", 10, 100, 40) # Slider to sele
|
|
159 |
bg_color = st.color_picker("Pick a background color for the text box:", "#FFFFFF")
|
160 |
font_color = st.color_picker("Pick a font color:", "#000000")
|
161 |
|
|
|
|
|
|
|
162 |
# Position options for the message (vertical and horizontal)
|
163 |
position_vertical = st.radio("Select message vertical position:", ["Top", "Center", "Bottom"])
|
164 |
position_horizontal = st.radio("Select message horizontal position:", ["Left", "Center", "Right"])
|
165 |
|
|
|
166 |
# Button to generate images
|
167 |
if st.button("Generate Image"):
|
168 |
if not prompt.strip():
|
@@ -182,14 +233,14 @@ if st.button("Generate Image"):
|
|
182 |
# Add the holiday message to the generated image
|
183 |
image_with_message = add_custom_message(
|
184 |
generated_image.copy(), message, font_path, font_size,
|
185 |
-
position_vertical, position_horizontal, max_chars, bg_color, font_color
|
186 |
)
|
187 |
|
188 |
# Display the image with the message
|
189 |
st.image(image_with_message, caption=f"Generated using {model_choice} with custom message", use_column_width=True)
|
190 |
|
191 |
# Preview the placement
|
192 |
-
st.write(f"Message preview (vertical position: {position_vertical}, horizontal position: {position_horizontal}, font: {font_choice}, size: {font_size}, max chars: {max_chars}, bg color: {bg_color}, font color: {font_color})")
|
193 |
|
194 |
except Exception as e:
|
195 |
st.error(f"An error occurred: {e}")
|
|
|
5 |
from io import BytesIO
|
6 |
import streamlit as st
|
7 |
import textwrap
|
8 |
+
from PIL import ImageColor
|
9 |
+
|
10 |
|
11 |
# Set page configuration
|
12 |
st.set_page_config(page_title="FLUX Image Generation Tool", page_icon="🎇")
|
|
|
57 |
"A lively Independence Day celebration with colorful fireworks lighting up the night sky, flags waving in the breeze, and a peaceful lakeside reflecting the celebration"
|
58 |
]
|
59 |
|
60 |
+
# Example messages corresponding to each holiday prompt
|
61 |
+
example_messages = [
|
62 |
+
"Wishing you peace and serenity this winter season. May the snow bring you joy and tranquility!",
|
63 |
+
"May your Christmas be as cozy and warm as a cabin in the woods, filled with holiday lights and love.",
|
64 |
+
"Warm wishes for the holidays! May your hearth and home be filled with love and joy this season.",
|
65 |
+
"Happy Hanukkah! May the light of the menorah shine brightly and bring joy to your heart.",
|
66 |
+
"Wishing you a vibrant New Year's Eve filled with excitement, fireworks, and joy for the year ahead!",
|
67 |
+
"May the magic and mystery of Samhain fill your spirit with wonder. Have a mystical holiday!",
|
68 |
+
"Celebrating the joy of Día de Muertos! May the memories of your loved ones bring peace and happiness.",
|
69 |
+
"Wishing you a Yule season filled with the warmth of the Norse tradition and the beauty of snowy forests.",
|
70 |
+
"Happy Diwali! May the lights of the diyas and the colors of rangoli bring you prosperity and joy.",
|
71 |
+
"Wishing you a reflective and peaceful Kwanzaa. May your home be filled with love, light, and unity.",
|
72 |
+
"Happy Thanksgiving! May your table be filled with gratitude, and your heart with the warmth of togetherness.",
|
73 |
+
"Wishing you a peaceful Winter Solstice night under the full moon. May the season bring you reflection and rest.",
|
74 |
+
"May the luck of the Irish be with you! Wishing you a joyful St. Patrick's Day filled with rainbows and gold.",
|
75 |
+
"Happy Lunar New Year! May the dragon dancers bring joy, and the fireworks light up a prosperous new year.",
|
76 |
+
"Wishing you a peaceful Easter surrounded by blooming flowers and the warmth of springtime sunshine.",
|
77 |
+
"Happy Valentine's Day! May your day be filled with love, glowing candles, and heartwarming moments.",
|
78 |
+
"Wishing you a joyful Holi! May the colors of the festival fill your life with vibrancy and happiness.",
|
79 |
+
"Ramadan Mubarak! May the crescent moon bring you peace, and the lanterns light up your spiritual path.",
|
80 |
+
"Happy Independence Day! May the fireworks light up the sky, and may freedom continue to inspire us all."
|
81 |
+
]
|
82 |
+
|
83 |
|
84 |
|
85 |
# Function to make requests to the FLUX models
|
|
|
115 |
return "\n".join(textwrap.fill(line, width=max_chars) for line in text.split("\n"))
|
116 |
|
117 |
# Function to add the holiday message to the image
|
118 |
+
from PIL import ImageColor, ImageDraw, ImageFont, Image
|
119 |
+
|
120 |
+
# Function to add the holiday message to the image
|
121 |
+
def add_custom_message(image, message, font_path, font_size, position_vertical, position_horizontal, max_chars, bg_color, font_color, alpha):
|
122 |
try:
|
123 |
font = ImageFont.truetype(font_path, font_size)
|
124 |
except IOError:
|
125 |
font = ImageFont.load_default()
|
126 |
|
127 |
+
# Convert image to RGBA if it's not already
|
128 |
+
if image.mode != "RGBA":
|
129 |
+
image = image.convert("RGBA")
|
130 |
+
|
131 |
+
# Create an overlay image with the same size and transparency (RGBA mode)
|
132 |
+
overlay = Image.new("RGBA", image.size, (255, 255, 255, 0)) # Fully transparent
|
133 |
+
|
134 |
+
draw = ImageDraw.Draw(overlay)
|
135 |
|
136 |
# Wrap the text based on the max_chars input
|
137 |
message = wrap_text(message, max_chars)
|
|
|
140 |
|
141 |
# Calculate the text bounding box dynamically
|
142 |
text_lines = message.split("\n")
|
143 |
+
line_height = draw.textbbox((0, 0), "A", font=font)[3] # Get height of one line of text
|
144 |
total_text_height = line_height * len(text_lines)
|
145 |
text_width = max([draw.textbbox((0, 0), line, font=font)[2] for line in text_lines])
|
146 |
|
|
|
163 |
# Define the padding for the background box behind the text
|
164 |
padding = 10
|
165 |
|
166 |
+
# Convert the user-specified background color to RGBA and apply the alpha transparency value
|
167 |
+
bg_color_rgba = (*ImageColor.getrgb(bg_color), alpha) # Apply user-controlled transparency
|
168 |
+
|
169 |
+
# Draw the semi-transparent background rectangle on the overlay
|
170 |
+
draw.rectangle([x_pos - padding, y_pos - padding, x_pos + text_width + padding, y_pos + total_text_height + padding], fill=bg_color_rgba)
|
171 |
|
172 |
# Draw the text line by line with the user-specified font color
|
173 |
for i, line in enumerate(text_lines):
|
174 |
draw.text((x_pos, y_pos + i * line_height), line, font=font, fill=font_color)
|
175 |
|
176 |
+
# Composite the overlay with the original image
|
177 |
+
combined = Image.alpha_composite(image, overlay)
|
178 |
+
|
179 |
+
return combined.convert("RGB") # Convert back to RGB for saving/display
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
|
184 |
# Streamlit UI
|
185 |
st.title("FLUX Image Generation")
|
|
|
191 |
prompt = custom_prompt if selected_prompt == "Custom" else selected_prompt
|
192 |
|
193 |
# Dropdown to select the model
|
194 |
+
model_choice = st.selectbox("Select the model:", ["flux-1-schnell", "flux-1-dev"])
|
195 |
+
|
196 |
+
# Dropdown to select an example message or write a custom one
|
197 |
+
selected_message = st.selectbox("Choose an example message or write your own:", options=["Custom"] + example_messages)
|
198 |
+
message = st.text_input("Enter a holiday message to add:", value=selected_message if selected_message != "Custom" else "")
|
199 |
|
200 |
# Additional inputs for customizing the message
|
|
|
201 |
font_choice = st.selectbox("Select a font:", list(fonts.keys()))
|
202 |
font_size = st.slider("Select font size:", 10, 100, 40)
|
203 |
max_chars = st.slider("Max characters per line:", 10, 100, 40) # Slider to select character wrap limit
|
|
|
206 |
bg_color = st.color_picker("Pick a background color for the text box:", "#FFFFFF")
|
207 |
font_color = st.color_picker("Pick a font color:", "#000000")
|
208 |
|
209 |
+
# **Add a transparency slider for controlling the alpha channel**
|
210 |
+
alpha = st.slider("Select transparency level for the background (0: fully transparent, 255: fully opaque)", 0, 255, 220)
|
211 |
+
|
212 |
# Position options for the message (vertical and horizontal)
|
213 |
position_vertical = st.radio("Select message vertical position:", ["Top", "Center", "Bottom"])
|
214 |
position_horizontal = st.radio("Select message horizontal position:", ["Left", "Center", "Right"])
|
215 |
|
216 |
+
|
217 |
# Button to generate images
|
218 |
if st.button("Generate Image"):
|
219 |
if not prompt.strip():
|
|
|
233 |
# Add the holiday message to the generated image
|
234 |
image_with_message = add_custom_message(
|
235 |
generated_image.copy(), message, font_path, font_size,
|
236 |
+
position_vertical, position_horizontal, max_chars, bg_color, font_color, alpha
|
237 |
)
|
238 |
|
239 |
# Display the image with the message
|
240 |
st.image(image_with_message, caption=f"Generated using {model_choice} with custom message", use_column_width=True)
|
241 |
|
242 |
# Preview the placement
|
243 |
+
st.write(f"Message preview (vertical position: {position_vertical}, horizontal position: {position_horizontal}, font: {font_choice}, size: {font_size}, max chars: {max_chars}, bg color: {bg_color}, font color: {font_color}, transparency: {alpha})")
|
244 |
|
245 |
except Exception as e:
|
246 |
st.error(f"An error occurred: {e}")
|