Ad_emplate_Generator / ad_Template_generator.py
Gokce's picture
Upload 5 files
c566077
import streamlit as st
from PIL import Image, ImageDraw, ImageFont
def create_ad_Template(punchline, button_text, logo_image, main_image, punchline_font_size, button_font_size):
ad_Template_width, ad_Template_height = 800, 1000
ad_Template = Image.new("RGB", (ad_Template_width, ad_Template_height), "white")
main_image = main_image.resize((300, 300))
ad_Template.paste(main_image, (250, 300))
logo_size = (200, 150)
logo_image = logo_image.resize(logo_size)
if logo_image.mode != 'RGBA':
logo_image = logo_image.convert('RGBA')
logo_position = (300, 50)
ad_Template.paste(logo_image, logo_position, logo_image)
draw = ImageDraw.Draw(ad_Template)
font_path = 'fonts/arial.ttf'
punchline_font = ImageFont.truetype(font_path, punchline_font_size)
button_font = ImageFont.truetype(font_path, button_font_size)
draw.text((400, 650), punchline, fill="black", font=punchline_font, anchor="mm", spacing=10, align="center", width=400)
#button size based on text length and font
button_text_size = button_font.getmask(button_text).getbbox()[2:]
button_width = button_text_size[0] + 40
button_height = button_text_size[1] + 20
button_y_position = 800
button_x_position = 400 - button_width // 2
button_position = (button_x_position, button_y_position)
corner_radius = 10
draw.rounded_rectangle([button_position, (button_position[0] + button_width, button_position[1] + button_height)],
fill=(144, 238, 144), outline="black", radius=corner_radius)
text_position = (button_x_position + button_width // 2, button_y_position + button_height // 2)
draw.text(text_position, button_text, fill="black", font=button_font, anchor="mm")
return ad_Template
def show_ad_Template_generator_page():
st.title("Ad Template Generator")
punchline = st.text_input("Enter the punchline text for the Ad Template:")
punchline_font_size = st.select_slider("Select the font size for the punchline:", options=[i for i in range(1, 101)])
button_text = st.text_input("Enter the button text:")
button_font_size = st.select_slider("Select the font size for the button text:", options=[i for i in range(1, 101)])
# Option to use the generated image or upload a new one
use_generated_image = st.checkbox("Use the image generated by Stable Diffusion")
logo_image = st.file_uploader("Choose a logo image...", type=["jpg", "jpeg", "png"])
if logo_image is not None:
logo_image = Image.open(logo_image)
main_image = None
if use_generated_image and 'generated_image' in st.session_state:
main_image = st.session_state['generated_image']
st.image(main_image, caption="Generated Image", use_column_width=True)
else:
uploaded_main_image = st.file_uploader("Choose the main image...", type=["jpg", "jpeg", "png"])
if uploaded_main_image is not None:
main_image = Image.open(uploaded_main_image)
generate_button = st.button("Generate Ad Template")
if generate_button and punchline and button_text and logo_image and main_image:
ad_Template = create_ad_Template(punchline, button_text, logo_image, main_image, punchline_font_size, button_font_size)
st.image(ad_Template, caption="Generated Ad Template", use_column_width=True)