Pratap2002 commited on
Commit
2b3ff28
·
verified ·
1 Parent(s): 72c4dbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -76
app.py CHANGED
@@ -141,6 +141,12 @@ def generate_poster():
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
@@ -162,10 +168,12 @@ def generate_poster():
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):
@@ -182,7 +190,7 @@ def generate_poster():
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
@@ -192,88 +200,56 @@ def generate_poster():
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)
209
- elif logo_position == "Top Right":
210
- position = (image.width - logo_width - padding, padding)
211
- elif logo_position == "Left Middle":
212
- position = (padding, (image.height - logo_height) // 2)
213
- elif logo_position == "Right Middle":
214
- position = (image.width - logo_width - padding, (image.height - logo_height) // 2)
215
- elif logo_position == "Bottom Left":
216
- position = (padding, image.height - logo_height - padding)
217
- elif logo_position == "Bottom Middle":
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
 
 
141
  ])
142
 
143
  description = st.text_area("Enter prompt for Advertisement:")
144
+ if st.button("Use Advertisement Generator", key="ad_generator_button"):
145
+ st.session_state.show_ad_generator = True
146
+
147
+ if st.session_state.get('show_ad_generator', False):
148
+ with st.expander("Advertisement Generator :arrow_right:", expanded=True):
149
+ advertisement_generator()
150
  header = st.text_input("Enter Header for Advertisement:")
151
  sub_header = st.text_input("Enter Sub-header for Advertisement:")
152
  # Allow multiple user prompts
 
168
  num_images = st.number_input("Number of Images", min_value=4, max_value=8, value=4)
169
 
170
  if st.button("Generate Images"):
171
+ base_prompt = f"Create a minimalistic and realistic poster image with the following text as the header: '{header}'. Use '{sub_header}' as the sub-header. Use this designe style and go with thi {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. Make the design clean and elegant, focusing on simplicity and realism rather than vibrant colors."
172
+
173
  if post_type == "Other":
174
+ full_prompt = f"{base_prompt} The image should be suitable for general use across various platforms."
175
  else:
176
+ full_prompt = f"{base_prompt} This image is specifically for a {post_type.lower()}."
177
 
178
  # Add user prompts to full_prompt if provided
179
  for i, user_prompt in enumerate(user_prompts):
 
190
  if image_bytes:
191
  image = Image.open(io.BytesIO(image_bytes))
192
 
193
+ # Add logo if provided
194
  if logo:
195
  logo_image = Image.open(logo)
196
  logo_width = int(image.width * 0.15) # 15% of the image width
 
200
  padding = int(image.width * 0.02) # Fixed 2% padding
201
 
202
  if logo_position == "None":
203
+ # Randomly choose a corner for logo placement
204
+ corner = random.choice(["Top Left", "Top Right", "Bottom Left", "Bottom Right"])
205
+ if corner == "Top Left":
206
+ position = (padding, padding)
207
+ elif corner == "Top Right":
208
+ position = (image.width - logo_width - padding, padding)
209
+ elif corner == "Bottom Left":
210
+ position = (padding, image.height - logo_height - padding)
211
+ else: # Bottom Right
212
+ position = (image.width - logo_width - padding, image.height - logo_height - padding)
213
+ else:
214
+ if logo_position == "Top Left":
215
+ position = (padding, padding)
216
+ elif logo_position == "Top Middle":
217
+ position = ((image.width - logo_width) // 2, padding)
218
+ elif logo_position == "Top Right":
219
+ position = (image.width - logo_width - padding, padding)
220
+ elif logo_position == "Bottom Left":
221
+ position = (padding, image.height - logo_height - padding)
222
+ elif logo_position == "Bottom Middle":
223
+ position = ((image.width - logo_width) // 2, image.height - logo_height - padding)
224
+ else: # Bottom Right
225
+ position = (image.width - logo_width - padding, image.height - logo_height - padding)
 
 
 
 
 
 
 
226
 
227
+ # Create a new image with an alpha channel
228
+ combined_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
229
+ combined_image.paste(image, (0, 0))
230
+
231
  # Convert logo to RGBA if it's not already
232
  if logo_image.mode != 'RGBA':
233
  logo_image = logo_image.convert('RGBA')
234
 
235
  combined_image.paste(logo_image, position, logo_image)
236
+
237
+ # Convert back to RGB for compatibility
238
+ image = combined_image.convert('RGB')
239
 
240
  # Display generated image
241
+ st.image(image, caption=f"Generated Poster {i+1}", use_column_width=True)
242
 
243
+ # Provide download option for the generated image
244
+ buf = io.BytesIO()
245
+ image.save(buf, format="PNG")
246
+ byte_im = buf.getvalue()
247
+ st.download_button(
248
+ label=f"Download generated poster {i+1}",
249
+ data=byte_im,
250
+ file_name=f"generated_poster_{i+1}.png",
251
+ mime="image/png"
252
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  else:
254
  st.error(f"Failed to generate image {i+1}")
255