Spaces:
Sleeping
Sleeping
jitubutwal1441
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageColor
|
3 |
+
from io import BytesIO
|
4 |
+
from rembg import remove
|
5 |
+
import os
|
6 |
+
from typing import Tuple
|
7 |
+
from PIL.Image import Image as PILImage
|
8 |
+
from copy import deepcopy
|
9 |
+
|
10 |
+
|
11 |
+
class ImageProcessor:
|
12 |
+
def __init__(self, image):
|
13 |
+
self.image = image
|
14 |
+
|
15 |
+
def remove_background(self):
|
16 |
+
return remove(self.image)
|
17 |
+
|
18 |
+
@staticmethod
|
19 |
+
def change_background(image, background_alpha: float = 1.0, background_hex: str = "#000000"):
|
20 |
+
"""
|
21 |
+
image: PIL Image (RGBA)
|
22 |
+
matte: PIL Image (grayscale, if 255 it is foreground)
|
23 |
+
background_alpha: float
|
24 |
+
background_hex: string
|
25 |
+
"""
|
26 |
+
img = deepcopy(image)
|
27 |
+
if image.mode != "RGBA":
|
28 |
+
img = img.convert("RGBA")
|
29 |
+
|
30 |
+
background_color = ImageColor.getrgb(background_hex)
|
31 |
+
background_alpha = int(255 * background_alpha)
|
32 |
+
background = Image.new(
|
33 |
+
"RGBA", img.size, color=background_color + (background_alpha,))
|
34 |
+
background.paste(img, mask=ImageProcessor.matte)
|
35 |
+
return background
|
36 |
+
|
37 |
+
@staticmethod
|
38 |
+
def matte(img_input):
|
39 |
+
# Implement matte logic
|
40 |
+
pass
|
41 |
+
|
42 |
+
|
43 |
+
class ImageDownloader:
|
44 |
+
@staticmethod
|
45 |
+
def download_button(pil_image, filename: str, fmt: str, label="Download"):
|
46 |
+
if fmt not in ["jpg", "png"]:
|
47 |
+
raise ValueError(
|
48 |
+
"Unknown image format (Available: jpg, png - case sensitive)")
|
49 |
+
|
50 |
+
pil_format = "JPEG" if fmt == "jpg" else "PNG"
|
51 |
+
file_format = "jpg" if fmt == "jpg" else "png"
|
52 |
+
mime = "image/jpeg" if fmt == "jpg" else "image/png"
|
53 |
+
|
54 |
+
buf = BytesIO()
|
55 |
+
pil_image.save(buf, format=pil_format)
|
56 |
+
|
57 |
+
return st.download_button(
|
58 |
+
label=label,
|
59 |
+
data=buf.getvalue(),
|
60 |
+
file_name=f'{filename}.{file_format}',
|
61 |
+
mime=mime,
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
class ImageUploader:
|
66 |
+
@staticmethod
|
67 |
+
def upload_image():
|
68 |
+
return st.file_uploader(
|
69 |
+
label="Upload your photo here",
|
70 |
+
accept_multiple_files=False, type=["png", "jpg", "jpeg"],
|
71 |
+
)
|
72 |
+
|
73 |
+
|
74 |
+
class BackgroundColorSelector:
|
75 |
+
@staticmethod
|
76 |
+
def select_background_color():
|
77 |
+
return st.selectbox("Choose background color", [
|
78 |
+
"Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"
|
79 |
+
])
|
80 |
+
|
81 |
+
|
82 |
+
class App:
|
83 |
+
def __init__(self):
|
84 |
+
self.image_processor = None
|
85 |
+
self.image_uploader = ImageUploader()
|
86 |
+
self.background_selector = BackgroundColorSelector()
|
87 |
+
self.image_downloader = ImageDownloader()
|
88 |
+
self.hexmap = {
|
89 |
+
"Transparent (PNG)": "#000000",
|
90 |
+
"Black": "#000000",
|
91 |
+
"White": "#FFFFFF",
|
92 |
+
"Green": "#22EE22",
|
93 |
+
"Red": "#EE2222",
|
94 |
+
"Blue": "#2222EE",
|
95 |
+
}
|
96 |
+
|
97 |
+
def run(self):
|
98 |
+
st.title("Simple image editor")
|
99 |
+
st.write(
|
100 |
+
"Simply remove the background of your picture and fill it with a plain color")
|
101 |
+
|
102 |
+
uploaded_file = self.image_uploader.upload_image()
|
103 |
+
|
104 |
+
if uploaded_file:
|
105 |
+
self.display_original_photo(uploaded_file)
|
106 |
+
|
107 |
+
in_mode = self.background_selector.select_background_color()
|
108 |
+
in_submit = st.button("Submit")
|
109 |
+
|
110 |
+
if in_submit:
|
111 |
+
self.process_and_display_image(uploaded_file, in_mode)
|
112 |
+
|
113 |
+
def display_original_photo(self, uploaded_file):
|
114 |
+
with st.expander("Original photo", expanded=True):
|
115 |
+
st.image(uploaded_file)
|
116 |
+
|
117 |
+
def process_and_display_image(self, uploaded_file, in_mode):
|
118 |
+
img_input = Image.open(uploaded_file)
|
119 |
+
self.image_processor = ImageProcessor(img_input)
|
120 |
+
|
121 |
+
with st.spinner("Magic happening behind the scenes. Please wait..."):
|
122 |
+
alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0
|
123 |
+
img_output = self.image_processor.remove_background()
|
124 |
+
# img_output = self.image_processor.change_background(
|
125 |
+
# img_without_bg, alpha, in_mode)
|
126 |
+
|
127 |
+
with st.expander("Success!", expanded=True):
|
128 |
+
st.image(img_output)
|
129 |
+
uploaded_name = os.path.splitext(uploaded_file.name)[0]
|
130 |
+
self.image_downloader.download_button(
|
131 |
+
pil_image=img_output,
|
132 |
+
filename=uploaded_name,
|
133 |
+
fmt="png"
|
134 |
+
)
|
135 |
+
|
136 |
+
|
137 |
+
if __name__ == "__main__":
|
138 |
+
app = App()
|
139 |
+
app.run()
|