Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,98 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
import os
|
| 5 |
|
|
|
|
|
|
|
| 6 |
# Function to process the image
|
| 7 |
-
def process_image(
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
output = rembg.remove(input_img)
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
return combined_img
|
| 18 |
|
| 19 |
# Streamlit app
|
| 20 |
def main():
|
| 21 |
-
st.title("
|
| 22 |
|
| 23 |
# Select background image
|
| 24 |
-
background_img_file = st.file_uploader("Select
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
background_img = Image.open(background_img_file)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import io
|
| 4 |
+
|
| 5 |
+
from rembg import remove
|
| 6 |
+
import requests
|
| 7 |
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
from streamlit_cropperjs import st_cropperjs
|
| 12 |
+
|
| 13 |
# Function to process the image
|
| 14 |
+
def process_image(image):
|
| 15 |
+
|
| 16 |
+
print(image)
|
| 17 |
+
|
| 18 |
+
os.makedirs('original', exist_ok=True)
|
| 19 |
+
os.makedirs('masked', exist_ok=True)
|
| 20 |
+
image = image.convert("RGB")
|
| 21 |
+
|
| 22 |
+
image.save('original/image.jpg', format = "jpeg")
|
| 23 |
+
|
| 24 |
+
output_path = "masked/image.png"
|
| 25 |
+
|
| 26 |
+
with open(output_path, "wb") as f:
|
| 27 |
+
input = open('original/image.jpg', 'rb').read()
|
| 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 |
+
background_img.putalpha(160)
|
|
|
|
| 37 |
|
| 38 |
+
background_img = background_img.resize((image.width, image.height))
|
| 39 |
+
#image = image.resize((background_img.width, background_img.height))
|
| 40 |
+
|
| 41 |
+
background_img = background_img.convert("RGBA")
|
| 42 |
+
|
| 43 |
+
input_img = Image.open('original/image.jpg')
|
| 44 |
+
|
| 45 |
+
input_img = input_img.convert("RGBA")
|
| 46 |
+
|
| 47 |
+
combined_img = Image.alpha_composite(input_img, background_img)
|
| 48 |
+
|
| 49 |
+
combined_img = combined_img.convert('RGB')
|
| 50 |
+
# combined_img.save('masked/finale.jpg', format='jpeg')
|
| 51 |
+
|
| 52 |
+
foreground_img = Image.open(output_path)
|
| 53 |
+
combined_img.paste(foreground_img, (0,0), foreground_img)
|
| 54 |
+
|
| 55 |
+
combined_img = combined_img.convert('RGB')
|
| 56 |
+
# combined_img.save("masked/background_final.jpg", format="jpeg")
|
| 57 |
|
| 58 |
return combined_img
|
| 59 |
|
| 60 |
# Streamlit app
|
| 61 |
def main():
|
| 62 |
+
st.title("Watermelon PFP Generator - By Kaito")
|
| 63 |
|
| 64 |
# Select background image
|
| 65 |
+
background_img_file = st.file_uploader("Select your image", type=["jpg", "png"])
|
| 66 |
+
if background_img_file is not None:
|
| 67 |
+
|
| 68 |
+
background_img_file = background_img_file.read()
|
| 69 |
+
|
| 70 |
+
cropped_pic = st_cropperjs(pic=background_img_file, btn_text="Generate!", key="foo")
|
| 71 |
+
if cropped_pic:
|
| 72 |
+
# save the cropped pic
|
| 73 |
+
cropped_pic = Image.open(io.BytesIO(cropped_pic))
|
| 74 |
+
cropped_pic.save("cropped_pic.png")
|
| 75 |
+
|
| 76 |
+
# Load input and background images
|
| 77 |
+
background_img = cropped_pic
|
| 78 |
+
|
| 79 |
+
# Process the images
|
| 80 |
+
img = process_image(background_img)
|
| 81 |
|
| 82 |
+
# Display the combined image
|
| 83 |
+
st.image(img, caption="Combined Image", use_column_width=True)
|
|
|
|
| 84 |
|
| 85 |
+
img_bytes = BytesIO()
|
| 86 |
+
img.save(img_bytes, format='PNG')
|
| 87 |
+
img_bytes = img_bytes.getvalue()
|
| 88 |
|
| 89 |
+
# Add a download button
|
| 90 |
+
st.download_button(
|
| 91 |
+
label="Download Image",
|
| 92 |
+
data=img_bytes,
|
| 93 |
+
file_name="free_palestine.png",
|
| 94 |
+
mime="image/png",
|
| 95 |
+
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
main()
|