Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import PIL
|
3 |
+
import requests
|
4 |
+
import torch
|
5 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
|
6 |
+
from PIL import Image, ImageDraw, ImageFont,ImageOps
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
|
10 |
+
def generate_image(url,color):
|
11 |
+
model_id = "timbrooks/instruct-pix2pix"
|
12 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None)
|
13 |
+
pipe.to("cuda")
|
14 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
15 |
+
image = download_image(url)
|
16 |
+
prompt = f"create a similar image, change the object color to {color}"
|
17 |
+
images = pipe(prompt, image=image, num_inference_steps=10, image_guidance_scale=1).images
|
18 |
+
generated_image = images[0].save("generated_image.jpg")
|
19 |
+
return images[0]
|
20 |
+
|
21 |
+
|
22 |
+
def download_image(url):
|
23 |
+
image = Image.open(url)
|
24 |
+
image = ImageOps.exif_transpose(image)
|
25 |
+
image = image.convert("RGB")
|
26 |
+
return image
|
27 |
+
|
28 |
+
def create_frame_with_resized_image(frame_size, image,logo_path, output_path, new_image_size,logo_size,text,button,color):
|
29 |
+
# Create an empty white frame
|
30 |
+
frame = Image.new("RGB", frame_size, "white")
|
31 |
+
|
32 |
+
# Open the image to be placed in the frame
|
33 |
+
original_image = image
|
34 |
+
logo = Image.open(logo_path)
|
35 |
+
# Resize the image to the desired dimensions
|
36 |
+
resized_image = original_image.resize(new_image_size)
|
37 |
+
resized_logo = logo.resize(logo_size)
|
38 |
+
# Calculate the position to center the resized image in the frame
|
39 |
+
x_position = (frame.width - resized_image.width) // 2
|
40 |
+
y_position = (frame.height - resized_image.height) // 2
|
41 |
+
|
42 |
+
frame.paste(resized_logo,(200,0))
|
43 |
+
# Paste the resized image onto the frame
|
44 |
+
frame.paste(resized_image, (x_position, y_position))
|
45 |
+
|
46 |
+
draw = ImageDraw.Draw(frame)
|
47 |
+
font_size = 24
|
48 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
49 |
+
text_width, text_height = draw.textsize(text, font)
|
50 |
+
text_x = (frame.width - text_width) // 2
|
51 |
+
|
52 |
+
button_width, button_height = draw.textsize(button, font)
|
53 |
+
button_x = (frame.width - button_width) // 2
|
54 |
+
button_y = 500
|
55 |
+
# Add text to the image
|
56 |
+
draw.text((text_x, 450), text, font=font, fill= f"{color}")
|
57 |
+
|
58 |
+
# Save the result
|
59 |
+
|
60 |
+
frame_thickness = 5
|
61 |
+
draw.rectangle([(button_x - frame_thickness, button_y - frame_thickness),
|
62 |
+
(button_x + button_width + frame_thickness, button_y + button_height + frame_thickness)],
|
63 |
+
fill=f"{color}")
|
64 |
+
|
65 |
+
draw.text((button_x,500),button,font=font, fill= "white")
|
66 |
+
|
67 |
+
frame.save(output_path)
|
68 |
+
|
69 |
+
|
70 |
+
def main():
|
71 |
+
st.set_page_config(page_title="img 2 audio story")
|
72 |
+
st.header("Turn img into ad template")
|
73 |
+
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
74 |
+
color_input = st.text_input("Enter the color you want in the image")
|
75 |
+
st.write("You entered:",color_input)
|
76 |
+
|
77 |
+
if uploaded_file is not None and color_input is not None:
|
78 |
+
print(uploaded_file)
|
79 |
+
bytes_data = uploaded_file.getvalue()
|
80 |
+
with open(uploaded_file.name, "wb") as file:
|
81 |
+
file.write(bytes_data)
|
82 |
+
st.image(uploaded_file,caption="Uploaded Image.",
|
83 |
+
use_column_width=True)
|
84 |
+
|
85 |
+
|
86 |
+
#st.image(generated_image,caption="Generated Image.", use_column_width=True)
|
87 |
+
|
88 |
+
#prompt_input = st.text_input("Enter the prompt:")
|
89 |
+
#st.write("You entered:", prompt_input)
|
90 |
+
punchline_input = st.text_input("Enter the punchline:")
|
91 |
+
st.write("You entered:", punchline_input)
|
92 |
+
|
93 |
+
button_input = st.text_input("Enter the button text:")
|
94 |
+
st.write("You entered:", button_input)
|
95 |
+
|
96 |
+
button_color_input = st.text_input("Enter the text and button color:")
|
97 |
+
st.write("You entered:",button_color_input)
|
98 |
+
|
99 |
+
uploaded_logo = st.file_uploader("Upload the logo", type="jpg")
|
100 |
+
if button_color_input is not None and uploaded_logo is not None and button_input is not None and punchline_input is not None:
|
101 |
+
frame_size = (600, 600)
|
102 |
+
logo_data = uploaded_logo.getvalue()
|
103 |
+
with open(uploaded_logo.name, "wb") as file:
|
104 |
+
file.write(logo_data)
|
105 |
+
|
106 |
+
output_path = "result.jpg"
|
107 |
+
new_image_size = (400, 300) # Adjust these dimensions as needed
|
108 |
+
logo_size = (200,150)
|
109 |
+
image = generate_image(uploaded_file.name,color_input)
|
110 |
+
logo_path = uploaded_logo.name
|
111 |
+
create_frame_with_resized_image(frame_size, image,logo_path, output_path, new_image_size,logo_size,punchline_input,button_input,button_color_input)
|
112 |
+
ad_template = Image.open(output_path)
|
113 |
+
st.image(ad_template,caption="Generated Ad Template", use_column_width=True)
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
main()
|
117 |
+
|
118 |
+
|
119 |
+
|