File size: 5,033 Bytes
1c2c1c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import streamlit as st
from rembg import remove
from PIL import Image, ImageOps
import numpy as np
# Set page config first
st.set_page_config(page_title="Background Removal App", layout="wide")
# Dark mod için CSS stilleri
st.markdown("""
<style>
/* Genel stil */
body {
background-color: black;
color: white;
font-family: 'Arial', sans-serif;
}
/* Buton stilleri */
.stDownloadButton button {
background-color: #4F8BF9;
color: white;
border: none;
padding: 12px 24px;
border-radius: 8px;
transition: background-color 0.3s ease;
font-size: 16px;
font-weight: bold;
}
.stDownloadButton button:hover {
background-color: #3B6BB0;
}
/* Footer stilleri */
.footer {
text-align: center;
padding: 10px;
background-color: #333;
border-radius: 8px;
margin-top: 20px;
}
/* Tooltip stilleri */
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%; /* Butonun üstünde */
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
""", unsafe_allow_html=True)
# App title and description
st.title("🎨 Modern Background Removal App")
st.write("""
📸 **Upload an image or use your camera to see the background removed results!**
This app is developed using the `rembg` library.
""")
# Yükleme ekranını daha iyi bir yere taşı
with st.expander("📁 Upload Image or Use Camera", expanded=True):
option = st.radio("Choose Input Method:", ("Upload Image", "Use Camera"))
if option == "Upload Image":
uploaded_file = st.file_uploader("Select an image...", type=["jpg", "jpeg", "png"])
else:
uploaded_file = st.camera_input("Take a photo")
if uploaded_file is not None:
# Open and display the original image
image = Image.open(uploaded_file)
st.subheader("🖼️ Original Image")
st.image(image, use_container_width=True, caption="Uploaded Image")
# Image manipulation options
st.subheader("🛠️ Image Adjustment")
rotate_angle = st.slider("Rotate Image (Degrees)", -180, 180, 0, help="Rotate the image by the specified angle.")
resize_width = st.slider("Resize Width", 100, 1000, image.width, help="Resize the image width while maintaining aspect ratio.")
# Apply image transformations
if rotate_angle != 0:
image = image.rotate(rotate_angle, resample=Image.Resampling.BICUBIC)
if resize_width != image.width:
ratio = resize_width / image.width
height = int(image.height * ratio)
image = image.resize((resize_width, height), resample=Image.Resampling.LANCZOS)
# Display the adjusted image
st.image(image, use_container_width=True, caption="Adjusted Image")
# Process image to remove background
with st.spinner("⏳ Removing background... Please wait."):
output_image = remove(image)
# Display background removed image
st.subheader("✅ Background Removed Image")
st.image(output_image, use_container_width=True, caption="Background Removed Image")
# Create silhouette
silhouette = np.array(output_image)
silhouette[silhouette[..., -1] > 0] = [255, 255, 255, 255] # Foreground white
silhouette[silhouette[..., -1] == 0] = [0, 0, 0, 255] # Background black
# Display silhouette
st.subheader("⚫ Silhouette")
st.image(silhouette, use_container_width=True, caption="Silhouette Image")
# Download buttons for results
st.markdown("### 📥 Download Options")
# Download background removed image
st.download_button(
label="⬇️ Download Background Removed Image",
data=output_image.tobytes(),
file_name="foreground_image.png",
mime="image/png",
key="download_foreground",
help="Download the image with the background removed."
)
# Download silhouette
silhouette_img = Image.fromarray(silhouette)
st.download_button(
label="⬇️ Download Silhouette",
data=silhouette_img.tobytes(),
file_name="silhouette_image.png",
mime="image/png",
key="download_silhouette",
help="Download the silhouette image."
)
# Footer
st.markdown("---")
st.markdown("""
<div class="footer">
🚀 **Developed by Metin Haşimi.**<br>
🌟 Connect with me on <a href="https://www.linkedin.com/in/metin-hasimi-33aa82269" target="_blank">LinkedIn</a> or check out my <a href="https://github.com/metinhasimi22" target="_blank">GitHub</a>.
</div>
""", unsafe_allow_html=True) |