Update app.py
Browse files
app.py
CHANGED
|
@@ -1,201 +1,19 @@
|
|
| 1 |
-
import io
|
| 2 |
-
|
| 3 |
-
import cv2
|
| 4 |
-
import numpy as np
|
| 5 |
import streamlit as st
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
st.markdown(
|
| 12 |
-
"""
|
| 13 |
-
<style>
|
| 14 |
-
@import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,400;9..144,600&family=Inter:wght@400;500;600&display=swap');
|
| 15 |
-
|
| 16 |
-
:root {
|
| 17 |
-
--ink: #1E2A22;
|
| 18 |
-
--panel: #24332A;
|
| 19 |
-
--paper: #F4EFE2;
|
| 20 |
-
--paper-dim: #E9E1CC;
|
| 21 |
-
--thread: #B23A2E;
|
| 22 |
-
--thread-dark: #8F2C22;
|
| 23 |
-
--brass: #C79A4B;
|
| 24 |
-
--text-on-ink: #F1ECDD;
|
| 25 |
-
--text-on-paper: #23281F;
|
| 26 |
-
--line-on-ink: rgba(241, 236, 221, 0.16);
|
| 27 |
-
--line-on-paper: rgba(35, 40, 31, 0.14);
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
html, body, [data-testid="stAppViewContainer"] {
|
| 31 |
-
background-color: var(--ink);
|
| 32 |
-
color: var(--text-on-ink);
|
| 33 |
-
font-family: 'Inter', sans-serif;
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
[data-testid="stHeader"] { background-color: transparent; }
|
| 37 |
-
|
| 38 |
-
[data-testid="stSidebar"] {
|
| 39 |
-
background-color: var(--panel);
|
| 40 |
-
border-right: 1px solid var(--line-on-ink);
|
| 41 |
-
}
|
| 42 |
-
[data-testid="stSidebar"] * { color: var(--text-on-ink) !important; }
|
| 43 |
-
|
| 44 |
-
.cw-hero {
|
| 45 |
-
background-image:
|
| 46 |
-
repeating-linear-gradient(0deg, var(--line-on-ink) 0 1px, transparent 1px 32px),
|
| 47 |
-
repeating-linear-gradient(90deg, var(--line-on-ink) 0 1px, transparent 1px 32px);
|
| 48 |
-
border-bottom: 1px solid var(--line-on-ink);
|
| 49 |
-
padding: 2.75rem 0.5rem 2.25rem 0.5rem;
|
| 50 |
-
margin: -1rem -1rem 2rem -1rem;
|
| 51 |
-
text-align: left;
|
| 52 |
-
}
|
| 53 |
-
.cw-hero h1 {
|
| 54 |
-
font-family: 'Fraunces', serif;
|
| 55 |
-
font-weight: 600;
|
| 56 |
-
font-size: 2.75rem;
|
| 57 |
-
letter-spacing: -0.01em;
|
| 58 |
-
margin: 0 0 0.4rem 0;
|
| 59 |
-
color: var(--text-on-ink);
|
| 60 |
-
}
|
| 61 |
-
.cw-hero p {
|
| 62 |
-
font-size: 1.02rem;
|
| 63 |
-
max-width: 46ch;
|
| 64 |
-
color: var(--text-on-ink);
|
| 65 |
-
opacity: 0.78;
|
| 66 |
-
margin: 0 0 0.9rem 0;
|
| 67 |
-
}
|
| 68 |
-
.cw-stitch {
|
| 69 |
-
border: none;
|
| 70 |
-
border-top: 2px dashed var(--brass);
|
| 71 |
-
width: 64px;
|
| 72 |
-
margin: 0 0 0.9rem 0;
|
| 73 |
-
opacity: 0.85;
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
.cw-sidebar-title {
|
| 77 |
-
font-family: 'Fraunces', serif;
|
| 78 |
-
font-size: 1.15rem;
|
| 79 |
-
font-weight: 600;
|
| 80 |
-
margin-bottom: 0.15rem;
|
| 81 |
-
}
|
| 82 |
-
.cw-sidebar-sub {
|
| 83 |
-
font-size: 0.82rem;
|
| 84 |
-
opacity: 0.65;
|
| 85 |
-
margin-bottom: 1.1rem;
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
.cw-chip {
|
| 89 |
-
display: inline-block;
|
| 90 |
-
font-size: 0.78rem;
|
| 91 |
-
font-weight: 500;
|
| 92 |
-
letter-spacing: 0.02em;
|
| 93 |
-
color: var(--text-on-paper);
|
| 94 |
-
background: var(--brass);
|
| 95 |
-
padding: 0.2rem 0.6rem;
|
| 96 |
-
border-radius: 3px;
|
| 97 |
-
margin-bottom: 0.6rem;
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
[data-testid="column"] > div {
|
| 101 |
-
background-color: var(--paper);
|
| 102 |
-
border: 1px solid var(--line-on-paper);
|
| 103 |
-
border-radius: 6px;
|
| 104 |
-
padding: 1.1rem 1.1rem 1.4rem 1.1rem;
|
| 105 |
-
}
|
| 106 |
-
[data-testid="column"] * :not(.cw-chip) { color: var(--text-on-paper); }
|
| 107 |
-
[data-testid="stImage"] img {
|
| 108 |
-
border-radius: 3px;
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
.stButton > button, [data-testid="stDownloadButton"] button {
|
| 112 |
-
background-color: var(--thread);
|
| 113 |
-
color: var(--text-on-ink) !important;
|
| 114 |
-
border: none;
|
| 115 |
-
border-radius: 4px;
|
| 116 |
-
padding: 0.55rem 1.1rem;
|
| 117 |
-
font-weight: 500;
|
| 118 |
-
transition: background-color 0.15s ease;
|
| 119 |
-
}
|
| 120 |
-
.stButton > button:hover, [data-testid="stDownloadButton"] button:hover {
|
| 121 |
-
background-color: var(--thread-dark);
|
| 122 |
-
}
|
| 123 |
-
.stButton > button:focus-visible, [data-testid="stDownloadButton"] button:focus-visible {
|
| 124 |
-
outline: 2px solid var(--brass);
|
| 125 |
-
outline-offset: 2px;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
[data-testid="stSlider"] [role="slider"] { background-color: var(--thread) !important; }
|
| 129 |
-
[data-testid="stSlider"] div[style*="background-color: rgb(255, 75, 75)"] { background-color: var(--thread) !important; }
|
| 130 |
-
|
| 131 |
-
[data-testid="stFileUploaderDropzone"] {
|
| 132 |
-
background-color: var(--panel);
|
| 133 |
-
border: 1px dashed var(--line-on-ink);
|
| 134 |
-
border-radius: 6px;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
footer, #MainMenu { visibility: hidden; }
|
| 138 |
-
</style>
|
| 139 |
-
|
| 140 |
-
<div class="cw-hero">
|
| 141 |
-
<h1>Cutaway</h1>
|
| 142 |
-
<hr class="cw-stitch" />
|
| 143 |
-
<p>Pull a clean, transparent cutout of a garment — ready for a catalogue, a listing, or a spec sheet.</p>
|
| 144 |
-
</div>
|
| 145 |
-
""",
|
| 146 |
-
unsafe_allow_html=True,
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
# ---------------------------------------------------------------------------
|
| 150 |
-
# AI cutout (segmentation / matting)
|
| 151 |
-
# ---------------------------------------------------------------------------
|
| 152 |
-
AI_MODELS = {
|
| 153 |
-
"u2netp — light and fast (default)": "u2netp",
|
| 154 |
-
"u2net — slightly cleaner, ~2x memory": "u2net",
|
| 155 |
-
}
|
| 156 |
-
|
| 157 |
-
MAX_MASK_EDGE = 1024
|
| 158 |
-
|
| 159 |
-
@st.cache_resource(show_spinner=False)
|
| 160 |
-
def load_ai_session(model_name: str):
|
| 161 |
-
import onnxruntime as ort
|
| 162 |
-
from rembg.sessions import sessions_class
|
| 163 |
-
|
| 164 |
-
opts = ort.SessionOptions()
|
| 165 |
-
opts.enable_cpu_mem_arena = False
|
| 166 |
-
opts.intra_op_num_threads = 1
|
| 167 |
-
opts.inter_op_num_threads = 1
|
| 168 |
-
|
| 169 |
-
session_cls = {cls.name(): cls for cls in sessions_class}[model_name]
|
| 170 |
-
return session_cls(model_name, opts)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
@st.cache_data(show_spinner=False, max_entries=4)
|
| 174 |
-
def ai_cutout(image_bytes: bytes, model_name: str) -> bytes:
|
| 175 |
-
from rembg import remove
|
| 176 |
-
|
| 177 |
-
source = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 178 |
-
|
| 179 |
-
small = source.copy()
|
| 180 |
-
small.thumbnail((MAX_MASK_EDGE, MAX_MASK_EDGE), Image.LANCZOS)
|
| 181 |
-
|
| 182 |
-
raw_mask = remove(small, session=load_ai_session(model_name), only_mask=True)
|
| 183 |
-
|
| 184 |
-
channels = raw_mask.split()
|
| 185 |
-
mask = channels[-1] if len(channels) == 4 else raw_mask.convert("L")
|
| 186 |
-
|
| 187 |
-
result = source.convert("RGBA")
|
| 188 |
-
result.putalpha(mask.resize(source.size, Image.LANCZOS))
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
|
| 195 |
-
# ---------------------------------------------------------------------------
|
| 196 |
-
# Chroma key
|
| 197 |
-
# ---------------------------------------------------------------------------
|
| 198 |
def detect_background_hex(img_array: np.ndarray, border: int = 10) -> str:
|
|
|
|
|
|
|
|
|
|
| 199 |
h, w, _ = img_array.shape
|
| 200 |
border = min(border, h // 2, w // 2) or 1
|
| 201 |
samples = np.concatenate([
|
|
@@ -208,110 +26,73 @@ def detect_background_hex(img_array: np.ndarray, border: int = 10) -> str:
|
|
| 208 |
return "#{:02X}{:02X}{:02X}".format(*avg)
|
| 209 |
|
| 210 |
|
| 211 |
-
|
| 212 |
-
import chromakey
|
| 213 |
-
out, mask = chromakey.chroma_key(img_array, key_hex, tola=tola, tolb=tolb)
|
| 214 |
-
alpha_f = 1 - mask
|
| 215 |
-
|
| 216 |
-
final_rgb = out
|
| 217 |
-
if grow_px > 0:
|
| 218 |
-
fg_binary = (alpha_f > 0.5).astype(np.uint8)
|
| 219 |
-
core = cv2.erode(fg_binary, np.ones((3, 3), np.uint8), iterations=grow_px)
|
| 220 |
-
if core.any():
|
| 221 |
-
_, indices = distance_transform_edt(1 - core, return_indices=True)
|
| 222 |
-
grown_rgb = out[indices[0], indices[1]]
|
| 223 |
-
final_rgb = np.where(core[..., None].astype(bool), out, grown_rgb)
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
# UI
|
| 232 |
-
# ---------------------------------------------------------------------------
|
| 233 |
-
uploaded_file = st.file_uploader(
|
| 234 |
-
"Upload a photo", type=["png", "jpg", "jpeg"], label_visibility="collapsed"
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
if uploaded_file is None:
|
| 238 |
-
st.markdown(
|
| 239 |
-
'<p style="opacity:0.65; padding: 0 0.25rem;">Drop a garment photo above to get started.</p>',
|
| 240 |
-
unsafe_allow_html=True,
|
| 241 |
-
)
|
| 242 |
-
st.stop()
|
| 243 |
-
|
| 244 |
-
image_bytes = uploaded_file.getvalue()
|
| 245 |
-
original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 246 |
-
img_array = np.array(original_image)
|
| 247 |
-
|
| 248 |
-
st.sidebar.markdown('<div class="cw-sidebar-title">Extraction</div>', unsafe_allow_html=True)
|
| 249 |
-
st.sidebar.markdown(
|
| 250 |
-
'<div class="cw-sidebar-sub">AI cutout reads the garment\'s shape. '
|
| 251 |
-
'Chroma key reads one background colour — only use it on a real green screen.</div>',
|
| 252 |
-
unsafe_allow_html=True,
|
| 253 |
-
)
|
| 254 |
-
method = st.sidebar.radio(
|
| 255 |
-
"Method",
|
| 256 |
-
["AI cutout", "Chroma key"],
|
| 257 |
-
label_visibility="collapsed"
|
| 258 |
-
)
|
| 259 |
-
|
| 260 |
-
if method == "AI cutout":
|
| 261 |
-
model_label = st.sidebar.selectbox("Model", list(AI_MODELS), index=0)
|
| 262 |
-
model_name = AI_MODELS[model_label]
|
| 263 |
-
|
| 264 |
-
st.sidebar.markdown(
|
| 265 |
-
'<div class="cw-sidebar-sub" style="margin-top:0.6rem;">Runs single-threaded '
|
| 266 |
-
'with the ONNX memory arena off, and masks at 1024 px, to hold steady '
|
| 267 |
-
'memory near 320 MB. The download is still full resolution.</div>',
|
| 268 |
-
unsafe_allow_html=True,
|
| 269 |
-
)
|
| 270 |
-
else:
|
| 271 |
-
detected_hex = detect_background_hex(img_array)
|
| 272 |
-
st.sidebar.markdown(
|
| 273 |
-
f'<div class="cw-sidebar-sub">Detected background: {detected_hex}</div>',
|
| 274 |
-
unsafe_allow_html=True,
|
| 275 |
-
)
|
| 276 |
key_color_hex = st.sidebar.color_picker("Background color", detected_hex)
|
| 277 |
-
tola = st.sidebar.slider("
|
| 278 |
-
tolb = st.sidebar.slider("
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
col1, col2 = st.columns(2)
|
| 282 |
-
|
| 283 |
-
with col1:
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
with col2:
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import cv2
|
| 6 |
+
import chromakey
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
st.set_page_config(layout="wide", page_title="Outfit Extractor")
|
| 9 |
+
st.title("🟩 Outfit / Garment Extractor")
|
| 10 |
+
st.write("Upload a green screen product photo to isolate the garment using chroma keying.")
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def detect_background_hex(img_array: np.ndarray, border: int = 10) -> str:
|
| 14 |
+
"""Average the pixels along the image border (almost always background)
|
| 15 |
+
and return their color as a hex string, so the key color picker starts
|
| 16 |
+
on the actual background shade instead of a hardcoded pure green."""
|
| 17 |
h, w, _ = img_array.shape
|
| 18 |
border = min(border, h // 2, w // 2) or 1
|
| 19 |
samples = np.concatenate([
|
|
|
|
| 26 |
return "#{:02X}{:02X}{:02X}".format(*avg)
|
| 27 |
|
| 28 |
|
| 29 |
+
uploaded_file = st.file_uploader("Upload your green screen photo", type=["png", "jpg", "jpeg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if uploaded_file is not None:
|
| 32 |
+
original_image = Image.open(uploaded_file).convert("RGB")
|
| 33 |
+
detected_hex = detect_background_hex(np.array(original_image))
|
| 34 |
|
| 35 |
+
st.sidebar.header("Chroma Key Settings")
|
| 36 |
+
st.sidebar.caption(f"Detected background color: {detected_hex}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
key_color_hex = st.sidebar.color_picker("Background color", detected_hex)
|
| 38 |
+
tola = st.sidebar.slider("Edge softness (tola)", 1, 50, 10, help="Distance below which a pixel is fully kept as foreground.")
|
| 39 |
+
tolb = st.sidebar.slider("Edge softness (tolb)", tola + 1, 120, 60, help="Distance above which a pixel is fully treated as background. Raise this if uneven lighting leaves background pixels partially opaque.")
|
| 40 |
+
erode_px = st.sidebar.slider("Fringe removal strength", 0, 5, 2, help="Shrinks the cutout edge by this many pixels to strip out green-tinted spill pixels left over from the original background. Raise if you still see a green rim; lower if thin straps/details start disappearing.")
|
| 41 |
+
|
| 42 |
+
col1, col2 = st.columns(2)
|
| 43 |
+
|
| 44 |
+
with col1:
|
| 45 |
+
st.header("Original Image")
|
| 46 |
+
st.image(original_image, width="stretch")
|
| 47 |
+
|
| 48 |
+
with col2:
|
| 49 |
+
st.header("Extracted Garment")
|
| 50 |
+
with st.spinner("Calculating chroma key..."):
|
| 51 |
+
try:
|
| 52 |
+
img_array = np.array(original_image) # RGB, shape (H, W, 3)
|
| 53 |
+
|
| 54 |
+
# chroma_key() takes a hex string, returns (out, mask) — NOT a single RGBA array.
|
| 55 |
+
# out = RGB with the key color subtracted from every pixel in proportion to
|
| 56 |
+
# the mask — this "decontaminates" green spill at semi-transparent
|
| 57 |
+
# edges (anti-aliased pixels blended with the background in the
|
| 58 |
+
# original photo). Fully-opaque interior pixels are unaffected.
|
| 59 |
+
# mask = float array, 1.0 = background, 0.0 = foreground
|
| 60 |
+
out, mask = chromakey.chroma_key(
|
| 61 |
+
img_array, key_color_hex, tola=tola, tolb=tolb
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Use `out` (spill-decontaminated) for RGB, not the raw original — the raw
|
| 65 |
+
# pixels still carry a green tint at edges even where alpha is partial,
|
| 66 |
+
# which shows up as a green fringe once composited on any other background.
|
| 67 |
+
alpha_f = 1 - mask # 0..1, foreground=1
|
| 68 |
+
|
| 69 |
+
# Decontamination alone still leaves a faint green rim on real photos
|
| 70 |
+
# (uneven studio lighting, anti-aliased edges in the source image). Erode
|
| 71 |
+
# the foreground region by a few pixels so those contaminated edge pixels
|
| 72 |
+
# are dropped entirely instead of staying semi-transparent and green-tinted.
|
| 73 |
+
if erode_px > 0:
|
| 74 |
+
fg_binary = (alpha_f > 0.5).astype(np.uint8)
|
| 75 |
+
kernel = np.ones((3, 3), np.uint8)
|
| 76 |
+
eroded = cv2.erode(fg_binary, kernel, iterations=erode_px)
|
| 77 |
+
safe_zone = cv2.dilate(eroded, kernel, iterations=1)
|
| 78 |
+
alpha_f = alpha_f * safe_zone
|
| 79 |
+
|
| 80 |
+
alpha = np.uint8(np.clip(alpha_f, 0, 1) * 255)
|
| 81 |
+
rgba = np.dstack([out, alpha])
|
| 82 |
+
extracted_image = Image.fromarray(rgba, "RGBA")
|
| 83 |
+
|
| 84 |
+
st.image(extracted_image, width="stretch")
|
| 85 |
+
|
| 86 |
+
buf = io.BytesIO()
|
| 87 |
+
extracted_image.save(buf, format="PNG")
|
| 88 |
+
byte_im = buf.getvalue()
|
| 89 |
+
|
| 90 |
+
st.download_button(
|
| 91 |
+
label="Download Transparent PNG",
|
| 92 |
+
data=byte_im,
|
| 93 |
+
file_name="chromakey_cutout.png",
|
| 94 |
+
mime="image/png",
|
| 95 |
+
width="stretch",
|
| 96 |
+
)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
st.error(f"Error processing image: {e}")
|