Spaces:
Runtime error
Runtime error
CallmeKaito
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,11 @@ import os
|
|
11 |
from streamlit_cropperjs import st_cropperjs
|
12 |
|
13 |
# Function to process the image
|
|
|
|
|
|
|
|
|
|
|
14 |
def process_image(image):
|
15 |
|
16 |
print(image)
|
@@ -28,24 +33,26 @@ def process_image(image):
|
|
28 |
subject = remove(input)
|
29 |
f.write(subject)
|
30 |
|
31 |
-
|
32 |
-
|
33 |
palestine_bg = "https://flagdownload.com/wp-content/uploads/Flag_of_Palestine_Flat_Round-1024x1024.png"
|
34 |
background_img = Image.open(BytesIO(requests.get(palestine_bg).content))
|
35 |
|
36 |
# Create a semi-transparent overlay
|
37 |
-
overlay = Image.new('RGBA', background_img.size, (0, 0, 0, 120)) # Adjust the last value (120) to control transparency
|
38 |
-
background_img = Image.alpha_composite(background_img.convert('RGBA'), overlay)
|
39 |
|
40 |
background_img = background_img.resize((image.width, image.height))
|
41 |
#image = image.resize((background_img.width, background_img.height))
|
42 |
|
|
|
|
|
43 |
background_img = background_img.convert("RGBA")
|
44 |
|
45 |
input_img = Image.open('original/image.jpg')
|
46 |
|
47 |
input_img = input_img.convert("RGBA")
|
48 |
|
|
|
|
|
49 |
combined_img = Image.alpha_composite(input_img, background_img)
|
50 |
|
51 |
combined_img = combined_img.convert('RGB')
|
@@ -57,41 +64,69 @@ def process_image(image):
|
|
57 |
combined_img = combined_img.convert('RGB')
|
58 |
# combined_img.save("masked/background_final.jpg", format="jpeg")
|
59 |
|
60 |
-
return combined_img
|
61 |
|
62 |
# Streamlit app
|
63 |
def main():
|
64 |
-
st.title("Watermelon PFP Generator -
|
65 |
|
66 |
# Select background image
|
67 |
background_img_file = st.file_uploader("Select your image", type=["jpg", "png"])
|
68 |
-
if background_img_file is not None:
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
background_img_file = background_img_file.read()
|
71 |
|
72 |
-
cropped_pic = st_cropperjs(pic=background_img_file, btn_text="Generate!", key="foo")
|
73 |
if cropped_pic:
|
74 |
# save the cropped pic
|
75 |
cropped_pic = Image.open(io.BytesIO(cropped_pic))
|
76 |
cropped_pic.save("cropped_pic.png")
|
77 |
|
78 |
# Process the images
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
main()
|
|
|
11 |
from streamlit_cropperjs import st_cropperjs
|
12 |
|
13 |
# Function to process the image
|
14 |
+
|
15 |
+
# 1st picture: normal behaviour
|
16 |
+
|
17 |
+
# 2nd picture: normal picture and applying the palestinian flag with an opacity of 120
|
18 |
+
|
19 |
def process_image(image):
|
20 |
|
21 |
print(image)
|
|
|
33 |
subject = remove(input)
|
34 |
f.write(subject)
|
35 |
|
|
|
|
|
36 |
palestine_bg = "https://flagdownload.com/wp-content/uploads/Flag_of_Palestine_Flat_Round-1024x1024.png"
|
37 |
background_img = Image.open(BytesIO(requests.get(palestine_bg).content))
|
38 |
|
39 |
# Create a semi-transparent overlay
|
40 |
+
# overlay = Image.new('RGBA', background_img.size, (0, 0, 0, 120)) # Adjust the last value (120) to control transparency
|
41 |
+
# background_img = Image.alpha_composite(background_img.convert('RGBA'), overlay)
|
42 |
|
43 |
background_img = background_img.resize((image.width, image.height))
|
44 |
#image = image.resize((background_img.width, background_img.height))
|
45 |
|
46 |
+
image = image.convert("RGBA")
|
47 |
+
|
48 |
background_img = background_img.convert("RGBA")
|
49 |
|
50 |
input_img = Image.open('original/image.jpg')
|
51 |
|
52 |
input_img = input_img.convert("RGBA")
|
53 |
|
54 |
+
normal_img = Image.blend(image, background_img, .3).convert('RGB')
|
55 |
+
|
56 |
combined_img = Image.alpha_composite(input_img, background_img)
|
57 |
|
58 |
combined_img = combined_img.convert('RGB')
|
|
|
64 |
combined_img = combined_img.convert('RGB')
|
65 |
# combined_img.save("masked/background_final.jpg", format="jpeg")
|
66 |
|
67 |
+
return combined_img, normal_img
|
68 |
|
69 |
# Streamlit app
|
70 |
def main():
|
71 |
+
st.title("Watermelon PFP Generator - by Kaito")
|
72 |
|
73 |
# Select background image
|
74 |
background_img_file = st.file_uploader("Select your image", type=["jpg", "png"])
|
75 |
+
if background_img_file is not None:
|
76 |
+
|
77 |
+
# handle error if not picture
|
78 |
+
if background_img_file.type.split("/")[0] != "image":
|
79 |
+
st.error("Please upload an image file")
|
80 |
+
return
|
81 |
|
82 |
background_img_file = background_img_file.read()
|
83 |
|
84 |
+
cropped_pic = st_cropperjs(pic=background_img_file, btn_text="Generate!", size=1.0, key="foo")
|
85 |
if cropped_pic:
|
86 |
# save the cropped pic
|
87 |
cropped_pic = Image.open(io.BytesIO(cropped_pic))
|
88 |
cropped_pic.save("cropped_pic.png")
|
89 |
|
90 |
# Process the images
|
91 |
+
|
92 |
+
with st.spinner('Generating your fabulous π profile picture...'):
|
93 |
+
img, img2 = process_image(cropped_pic)
|
94 |
+
|
95 |
+
# Create two columns
|
96 |
+
col1, col2 = st.columns(2)
|
97 |
+
|
98 |
+
# Display the images in each column
|
99 |
+
with col1:
|
100 |
+
st.image(img, caption="Image 1", use_column_width=True)
|
101 |
+
|
102 |
+
img_bytes = BytesIO()
|
103 |
+
img.save(img_bytes, format='PNG')
|
104 |
+
img_bytes = img_bytes.getvalue()
|
105 |
+
|
106 |
+
# Add a download button
|
107 |
+
st.download_button(
|
108 |
+
label="Download Image 1",
|
109 |
+
data=img_bytes,
|
110 |
+
file_name="free_palestine.png",
|
111 |
+
mime="image/png",
|
112 |
+
)
|
113 |
+
|
114 |
+
with col2:
|
115 |
+
st.image(img2, caption="Image 2", use_column_width=True)
|
116 |
+
|
117 |
+
img_bytes = BytesIO()
|
118 |
+
img2.save(img_bytes, format='PNG')
|
119 |
+
img_bytes = img_bytes.getvalue()
|
120 |
+
|
121 |
+
# Add a download button
|
122 |
+
st.download_button(
|
123 |
+
label="Download Image 2",
|
124 |
+
data=img_bytes,
|
125 |
+
file_name="free_palestine2.png",
|
126 |
+
mime="image/png",
|
127 |
+
)
|
128 |
+
|
129 |
+
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
main()
|