Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -140,31 +140,38 @@ def generate_poster():
|
|
140 |
"Corporate/Professional", "Retro/Vintage", "Modern/Contemporary", "Illustrative/Artistic"
|
141 |
])
|
142 |
|
|
|
143 |
header = st.text_input("Enter Header for Advertisement:")
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
logo = st.file_uploader("Upload Logo (optional)", type=['png', 'jpg', 'jpeg'])
|
147 |
|
148 |
# Add logo position selection
|
149 |
logo_position = st.selectbox("Select Logo Position", [
|
150 |
-
"Top Left", "Top Middle", "Top Right",
|
151 |
"Left Middle", "Right Middle",
|
152 |
"Bottom Left", "Bottom Middle", "Bottom Right"
|
153 |
])
|
154 |
|
155 |
-
# Add padding slider
|
156 |
-
padding_percent = st.slider("Logo Padding (%)", 1, 10, 2)
|
157 |
-
|
158 |
num_images = st.number_input("Number of Images", min_value=4, max_value=8, value=4)
|
159 |
-
quality_factor = st.number_input("Quality Factor", min_value=1, max_value=4, value=1)
|
160 |
|
161 |
if st.button("Generate Images"):
|
162 |
if post_type == "Other":
|
163 |
-
full_prompt = f"{header}.
|
164 |
else:
|
165 |
-
full_prompt = f"{header}.
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
-
generated_images = []
|
168 |
for i in range(num_images):
|
169 |
with st.spinner(f"Generating image {i+1}..."):
|
170 |
logger.info(f"Generating image {i+1} with prompt: {full_prompt}")
|
@@ -174,20 +181,28 @@ def generate_poster():
|
|
174 |
|
175 |
if image_bytes:
|
176 |
image = Image.open(io.BytesIO(image_bytes))
|
177 |
-
if quality_factor > 1:
|
178 |
-
image = increase_image_quality(image, quality_factor)
|
179 |
|
180 |
-
# Add logo
|
181 |
if logo:
|
182 |
logo_image = Image.open(logo)
|
183 |
logo_width = int(image.width * 0.15) # 15% of the image width
|
184 |
logo_height = int(logo_image.height * (logo_width / logo_image.width))
|
185 |
logo_image = logo_image.resize((logo_width, logo_height), Image.LANCZOS)
|
186 |
|
187 |
-
padding = int(image.width *
|
188 |
|
189 |
-
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
position = (padding, padding)
|
192 |
elif logo_position == "Top Middle":
|
193 |
position = ((image.width - logo_width) // 2, padding)
|
@@ -203,38 +218,64 @@ def generate_poster():
|
|
203 |
position = ((image.width - logo_width) // 2, image.height - logo_height - padding)
|
204 |
else: # Bottom Right
|
205 |
position = (image.width - logo_width - padding, image.height - logo_height - padding)
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
211 |
# Convert logo to RGBA if it's not already
|
212 |
if logo_image.mode != 'RGBA':
|
213 |
logo_image = logo_image.convert('RGBA')
|
214 |
|
215 |
combined_image.paste(logo_image, position, logo_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
-
#
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
else:
|
222 |
-
st.error("Failed to generate image")
|
223 |
-
|
224 |
-
# Display generated images
|
225 |
-
for i, img in enumerate(generated_images):
|
226 |
-
st.image(img, caption=f"Generated Poster {i+1}", use_column_width=True)
|
227 |
-
|
228 |
-
# Provide download option for the generated image
|
229 |
-
buf = io.BytesIO()
|
230 |
-
img.save(buf, format="PNG")
|
231 |
-
byte_im = buf.getvalue()
|
232 |
-
st.download_button(
|
233 |
-
label=f"Download generated poster {i+1}",
|
234 |
-
data=byte_im,
|
235 |
-
file_name=f"generated_poster_{i+1}.png",
|
236 |
-
mime="image/png"
|
237 |
-
)
|
238 |
|
239 |
# Content from image_to_image.py
|
240 |
def encode_image(image):
|
|
|
140 |
"Corporate/Professional", "Retro/Vintage", "Modern/Contemporary", "Illustrative/Artistic"
|
141 |
])
|
142 |
|
143 |
+
description = st.text_area("Enter prompt for Advertisement:")
|
144 |
header = st.text_input("Enter Header for Advertisement:")
|
145 |
+
sub_header = st.text_input("Enter Sub-header for Advertisement:")
|
146 |
+
# Allow multiple user prompts
|
147 |
+
user_prompts = []
|
148 |
+
num_prompts = st.number_input("Number of Text Descriptions", min_value=1, max_value=80, value=1)
|
149 |
+
for i in range(num_prompts):
|
150 |
+
user_prompt = st.text_area(f"Enter Descriptions to display in the image (Descriptions {i+1}):")
|
151 |
+
user_prompts.append(user_prompt)
|
152 |
+
|
153 |
logo = st.file_uploader("Upload Logo (optional)", type=['png', 'jpg', 'jpeg'])
|
154 |
|
155 |
# Add logo position selection
|
156 |
logo_position = st.selectbox("Select Logo Position", [
|
157 |
+
"None", "Top Left", "Top Middle", "Top Right",
|
158 |
"Left Middle", "Right Middle",
|
159 |
"Bottom Left", "Bottom Middle", "Bottom Right"
|
160 |
])
|
161 |
|
|
|
|
|
|
|
162 |
num_images = st.number_input("Number of Images", min_value=4, max_value=8, value=4)
|
|
|
163 |
|
164 |
if st.button("Generate Images"):
|
165 |
if post_type == "Other":
|
166 |
+
full_prompt = f"Create an image with the following text as the header: '{header}'. Use '{sub_header}' as the sub-header. Use a {design_style.lower()} style for the post. Include the following text as the description: {description}. The image should have an aspect ratio of {aspect_ratio} and dimensions of {width}x{height}. Ensure the image is in 4k high resolution."
|
167 |
else:
|
168 |
+
full_prompt = f"Create a {post_type.lower()} with the following text as the header: '{header}'. Use '{sub_header}' as the sub-header. Use a {design_style.lower()} style for the post. Include the following text as the description: {description}. The image should have an aspect ratio of {aspect_ratio} and dimensions of {width}x{height}. Ensure the image is in 4k high resolution."
|
169 |
+
|
170 |
+
# Add user prompts to full_prompt if provided
|
171 |
+
for i, user_prompt in enumerate(user_prompts):
|
172 |
+
if user_prompt:
|
173 |
+
full_prompt += f" Include the following text in the image (Prompt {i+1}): '{user_prompt}'."
|
174 |
|
|
|
175 |
for i in range(num_images):
|
176 |
with st.spinner(f"Generating image {i+1}..."):
|
177 |
logger.info(f"Generating image {i+1} with prompt: {full_prompt}")
|
|
|
181 |
|
182 |
if image_bytes:
|
183 |
image = Image.open(io.BytesIO(image_bytes))
|
|
|
|
|
184 |
|
185 |
+
# Add logo if provided, or place it randomly if not specified
|
186 |
if logo:
|
187 |
logo_image = Image.open(logo)
|
188 |
logo_width = int(image.width * 0.15) # 15% of the image width
|
189 |
logo_height = int(logo_image.height * (logo_width / logo_image.width))
|
190 |
logo_image = logo_image.resize((logo_width, logo_height), Image.LANCZOS)
|
191 |
|
192 |
+
padding = int(image.width * 0.02) # Fixed 2% padding
|
193 |
|
194 |
+
if logo_position == "None":
|
195 |
+
# If "None" is selected, randomly choose a position
|
196 |
+
random_positions = [
|
197 |
+
(padding, padding), # Top Left
|
198 |
+
((image.width - logo_width) // 2, padding), # Top Middle
|
199 |
+
(image.width - logo_width - padding, padding), # Top Right
|
200 |
+
(padding, image.height - logo_height - padding), # Bottom Left
|
201 |
+
((image.width - logo_width) // 2, image.height - logo_height - padding), # Bottom Middle
|
202 |
+
(image.width - logo_width - padding, image.height - logo_height - padding) # Bottom Right
|
203 |
+
]
|
204 |
+
position = random.choice(random_positions)
|
205 |
+
elif logo_position == "Top Left":
|
206 |
position = (padding, padding)
|
207 |
elif logo_position == "Top Middle":
|
208 |
position = ((image.width - logo_width) // 2, padding)
|
|
|
218 |
position = ((image.width - logo_width) // 2, image.height - logo_height - padding)
|
219 |
else: # Bottom Right
|
220 |
position = (image.width - logo_width - padding, image.height - logo_height - padding)
|
221 |
+
|
222 |
+
# Create a new image with an alpha channel
|
223 |
+
combined_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
224 |
+
combined_image.paste(image, (0, 0))
|
225 |
+
|
226 |
+
if logo:
|
227 |
# Convert logo to RGBA if it's not already
|
228 |
if logo_image.mode != 'RGBA':
|
229 |
logo_image = logo_image.convert('RGBA')
|
230 |
|
231 |
combined_image.paste(logo_image, position, logo_image)
|
232 |
+
|
233 |
+
# Convert back to RGB for compatibility
|
234 |
+
image = combined_image.convert('RGB')
|
235 |
+
|
236 |
+
# Display generated image
|
237 |
+
col1, col2 = st.columns([3, 1])
|
238 |
+
|
239 |
+
with col1:
|
240 |
+
st.image(image, caption=f"Generated Poster {i+1}", use_column_width=True)
|
241 |
|
242 |
+
# Provide download option for the generated image
|
243 |
+
buf = io.BytesIO()
|
244 |
+
image.save(buf, format="PNG")
|
245 |
+
byte_im = buf.getvalue()
|
246 |
+
st.download_button(
|
247 |
+
label=f"Download generated poster {i+1}",
|
248 |
+
data=byte_im,
|
249 |
+
file_name=f"generated_poster_{i+1}.png",
|
250 |
+
mime="image/png"
|
251 |
+
)
|
252 |
|
253 |
+
with col2:
|
254 |
+
st.subheader("Color Options")
|
255 |
+
colors = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF", "#FFFFFF"]
|
256 |
+
color_names = ["Red", "Green", "Blue", "Yellow", "Magenta", "Cyan", "White"]
|
257 |
+
|
258 |
+
for color, name in zip(colors, color_names):
|
259 |
+
if st.button(name, key=f"{name}_{i}"):
|
260 |
+
# Create a new image with the selected color
|
261 |
+
colored_img = Image.new('RGB', image.size, color)
|
262 |
+
# Blend the original image with the colored image
|
263 |
+
blended_img = Image.blend(image, colored_img, 0.5)
|
264 |
+
# Display the blended image
|
265 |
+
st.image(blended_img, caption=f"Generated Poster {i+1} with {name} tint", use_column_width=True)
|
266 |
+
|
267 |
+
# Provide download option for the blended image
|
268 |
+
buf = io.BytesIO()
|
269 |
+
blended_img.save(buf, format="PNG")
|
270 |
+
byte_im = buf.getvalue()
|
271 |
+
st.download_button(
|
272 |
+
label=f"Download {name} tinted poster {i+1}",
|
273 |
+
data=byte_im,
|
274 |
+
file_name=f"generated_poster_{i+1}_{name}_tint.png",
|
275 |
+
mime="image/png"
|
276 |
+
)
|
277 |
else:
|
278 |
+
st.error(f"Failed to generate image {i+1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
280 |
# Content from image_to_image.py
|
281 |
def encode_image(image):
|