Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,248 +1,78 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from PIL import Image, ImageEnhance,
|
| 3 |
import numpy as np
|
| 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 |
-
class SharpnessFilter(ImageEditor):
|
| 46 |
-
def __init__(self, image, factor):
|
| 47 |
-
super().__init__(image)
|
| 48 |
-
self.factor = factor
|
| 49 |
-
|
| 50 |
-
def apply(self):
|
| 51 |
-
enhancer = ImageEnhance.Sharpness(self.image)
|
| 52 |
-
return enhancer.enhance(self.factor)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Saturation Adjustment Class
|
| 56 |
-
class SaturationFilter(ImageEditor):
|
| 57 |
-
def __init__(self, image, factor):
|
| 58 |
-
super().__init__(image)
|
| 59 |
-
self.factor = factor
|
| 60 |
-
|
| 61 |
-
def apply(self):
|
| 62 |
-
enhancer = ImageEnhance.Color(self.image)
|
| 63 |
-
return enhancer.enhance(self.factor)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# Blur Effect Class
|
| 67 |
-
class BlurFilter(ImageEditor):
|
| 68 |
-
def __init__(self, image, radius):
|
| 69 |
-
super().__init__(image)
|
| 70 |
-
self.radius = radius
|
| 71 |
-
|
| 72 |
-
def apply(self):
|
| 73 |
-
return self.image.filter(ImageFilter.GaussianBlur(self.radius))
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# Sepia Filter Class
|
| 77 |
-
class SepiaFilter(ImageEditor):
|
| 78 |
-
def apply(self):
|
| 79 |
-
width, height = self.image.size
|
| 80 |
-
pixels = self.image.load()
|
| 81 |
-
for py in range(height):
|
| 82 |
-
for px in range(width):
|
| 83 |
-
r, g, b = self.image.getpixel((px, py))
|
| 84 |
-
|
| 85 |
-
tr = int(0.393 * r + 0.769 * g + 0.189 * b)
|
| 86 |
-
tg = int(0.349 * r + 0.686 * g + 0.168 * b)
|
| 87 |
-
tb = int(0.272 * r + 0.534 * g + 0.131 * b)
|
| 88 |
-
|
| 89 |
-
# Apply limits to avoid overflow
|
| 90 |
-
if tr > 255:
|
| 91 |
-
tr = 255
|
| 92 |
-
if tg > 255:
|
| 93 |
-
tg = 255
|
| 94 |
-
if tb > 255:
|
| 95 |
-
tb = 255
|
| 96 |
-
|
| 97 |
-
pixels[px, py] = (tr, tg, tb)
|
| 98 |
-
|
| 99 |
-
return self.image
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# Emboss Filter Class
|
| 103 |
-
class EmbossFilter(ImageEditor):
|
| 104 |
-
def apply(self):
|
| 105 |
-
return self.image.filter(ImageFilter.EMBOSS)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
# Invert Filter Class
|
| 109 |
-
class InvertFilter(ImageEditor):
|
| 110 |
-
def apply(self):
|
| 111 |
-
return ImageOps.invert(self.image.convert('RGB'))
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# Border Class
|
| 115 |
-
class Border:
|
| 116 |
-
def __init__(self, image, border_type, color, thickness):
|
| 117 |
-
self.image = image
|
| 118 |
-
self.border_type = border_type
|
| 119 |
-
self.color = color
|
| 120 |
-
self.thickness = thickness
|
| 121 |
-
|
| 122 |
-
def add_border(self):
|
| 123 |
-
try:
|
| 124 |
-
border_color = self.hex_to_rgb(self.color)
|
| 125 |
-
except ValueError:
|
| 126 |
-
st.error("Invalid color format. Please use Hex color.")
|
| 127 |
-
return self.image
|
| 128 |
-
|
| 129 |
-
if self.border_type == "Solid Color":
|
| 130 |
-
return ImageOps.expand(self.image, border=self.thickness, fill=border_color)
|
| 131 |
-
elif self.border_type == "Shadow":
|
| 132 |
-
return self.add_shadow_border()
|
| 133 |
-
elif self.border_type == "Rounded":
|
| 134 |
-
return self.add_rounded_border()
|
| 135 |
-
return self.image
|
| 136 |
-
|
| 137 |
-
def hex_to_rgb(self, hex_color):
|
| 138 |
-
hex_color = hex_color.lstrip('#')
|
| 139 |
-
if len(hex_color) == 6:
|
| 140 |
-
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
|
| 141 |
-
else:
|
| 142 |
-
raise ValueError("Invalid hex color format")
|
| 143 |
-
|
| 144 |
-
def add_shadow_border(self):
|
| 145 |
-
shadow = Image.new("RGBA", (self.image.width + 20, self.image.height + 20), (0, 0, 0, 0))
|
| 146 |
-
shadow.paste(self.image, (10, 10))
|
| 147 |
-
return shadow.convert("RGB")
|
| 148 |
-
|
| 149 |
-
def add_rounded_border(self):
|
| 150 |
-
radius = 20
|
| 151 |
-
img_with_border = self.image.copy()
|
| 152 |
-
img_with_border = img_with_border.convert("RGBA")
|
| 153 |
-
mask = Image.new("L", img_with_border.size, 255)
|
| 154 |
-
draw = ImageDraw.Draw(mask)
|
| 155 |
-
draw.rounded_rectangle([0, 0, img_with_border.width, img_with_border.height], radius, fill=0)
|
| 156 |
-
img_with_border.putalpha(mask)
|
| 157 |
-
return img_with_border.convert("RGB")
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
# Main Application Class
|
| 161 |
-
class ImageApp:
|
| 162 |
-
def __init__(self):
|
| 163 |
-
self.image = None
|
| 164 |
-
self.stored_image = None
|
| 165 |
-
|
| 166 |
-
def load_image(self):
|
| 167 |
-
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 168 |
-
if uploaded_image:
|
| 169 |
-
self.image = Image.open(uploaded_image)
|
| 170 |
-
self.stored_image = self.image.copy()
|
| 171 |
-
|
| 172 |
-
def apply_filters(self):
|
| 173 |
-
if self.image:
|
| 174 |
-
# Sidebar Filters
|
| 175 |
-
filter_option = st.sidebar.selectbox("Choose a filter", ["None", "Grayscale", "Brightness", "Contrast",
|
| 176 |
-
"Sharpness", "Saturation", "Blur", "Sepia", "Emboss", "Invert"])
|
| 177 |
-
if filter_option == "Grayscale":
|
| 178 |
-
self.image = GrayscaleFilter(self.image).apply()
|
| 179 |
-
elif filter_option == "Brightness":
|
| 180 |
-
brightness_factor = st.sidebar.slider("Adjust Brightness", 0.1, 2.0, 1.0)
|
| 181 |
-
self.image = BrightnessFilter(self.image, brightness_factor).apply()
|
| 182 |
-
elif filter_option == "Contrast":
|
| 183 |
-
contrast_factor = st.sidebar.slider("Adjust Contrast", 0.1, 2.0, 1.0)
|
| 184 |
-
self.image = ContrastFilter(self.image, contrast_factor).apply()
|
| 185 |
-
elif filter_option == "Sharpness":
|
| 186 |
-
sharpness_factor = st.sidebar.slider("Adjust Sharpness", 0.1, 2.0, 1.0)
|
| 187 |
-
self.image = SharpnessFilter(self.image, sharpness_factor).apply()
|
| 188 |
-
elif filter_option == "Saturation":
|
| 189 |
-
saturation_factor = st.sidebar.slider("Adjust Saturation", 0.1, 2.0, 1.0)
|
| 190 |
-
self.image = SaturationFilter(self.image, saturation_factor).apply()
|
| 191 |
-
elif filter_option == "Blur":
|
| 192 |
-
blur_radius = st.sidebar.slider("Blur Radius", 1, 10, 2)
|
| 193 |
-
self.image = BlurFilter(self.image, blur_radius).apply()
|
| 194 |
-
elif filter_option == "Sepia":
|
| 195 |
-
self.image = SepiaFilter(self.image).apply()
|
| 196 |
-
elif filter_option == "Emboss":
|
| 197 |
-
self.image = EmbossFilter(self.image).apply()
|
| 198 |
-
elif filter_option == "Invert":
|
| 199 |
-
self.image = InvertFilter(self.image).apply()
|
| 200 |
-
|
| 201 |
-
def add_border(self):
|
| 202 |
-
if self.image:
|
| 203 |
-
border_type = st.sidebar.selectbox("Choose Border Type", ["None", "Solid Color", "Shadow", "Rounded"])
|
| 204 |
-
if border_type != "None":
|
| 205 |
-
border_color = st.sidebar.color_picker("Select Border Color", "#b33c3c")
|
| 206 |
-
border_thickness = st.sidebar.slider("Border Thickness", 1, 20, 5)
|
| 207 |
-
border = Border(self.image, border_type, border_color, border_thickness)
|
| 208 |
-
self.image = border.add_border()
|
| 209 |
-
|
| 210 |
-
def display_images(self):
|
| 211 |
-
if self.image:
|
| 212 |
-
col1, col2 = st.columns(2)
|
| 213 |
-
with col1:
|
| 214 |
-
st.image(self.stored_image, caption="Original Image", use_container_width=True)
|
| 215 |
-
with col2:
|
| 216 |
-
st.image(self.image, caption="Edited Image", use_container_width=True)
|
| 217 |
-
|
| 218 |
-
def download_button(self):
|
| 219 |
-
if self.image:
|
| 220 |
-
buffered = BytesIO()
|
| 221 |
-
self.image.save(buffered, format="PNG")
|
| 222 |
-
buffered.seek(0)
|
| 223 |
-
st.sidebar.download_button("Download Edited Image", buffered, "edited_image.png", "image/png")
|
| 224 |
|
|
|
|
|
|
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
st.title("Modernized Image Editor")
|
| 229 |
-
app = ImageApp()
|
| 230 |
|
| 231 |
-
|
| 232 |
-
app.load_image()
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
app.display_images()
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
if __name__ == "__main__":
|
| 248 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 3 |
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import cv2
|
| 6 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 7 |
+
|
| 8 |
+
# Load models for AI enhancements
|
| 9 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 10 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
+
|
| 12 |
+
# Function to enhance image quality using PIL
|
| 13 |
+
def enhance_image(image, enhance_type, factor):
|
| 14 |
+
enhancer = None
|
| 15 |
+
if enhance_type == 'Brightness':
|
| 16 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 17 |
+
elif enhance_type == 'Contrast':
|
| 18 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 19 |
+
elif enhance_type == 'Sharpness':
|
| 20 |
+
enhancer = ImageEnhance.Sharpness(image)
|
| 21 |
+
elif enhance_type == 'Color':
|
| 22 |
+
enhancer = ImageEnhance.Color(image)
|
| 23 |
+
|
| 24 |
+
if enhancer:
|
| 25 |
+
return enhancer.enhance(factor)
|
| 26 |
+
return image
|
| 27 |
+
|
| 28 |
+
# Function for applying filters (Grayscale, etc.)
|
| 29 |
+
def apply_filter(image, filter_type):
|
| 30 |
+
if filter_type == "Grayscale":
|
| 31 |
+
return image.convert("L")
|
| 32 |
+
elif filter_type == "Blur":
|
| 33 |
+
return image.filter(ImageFilter.GaussianBlur(radius=5))
|
| 34 |
+
return image
|
| 35 |
+
|
| 36 |
+
# Function to remove background (using a simple model or API)
|
| 37 |
+
def remove_background(image):
|
| 38 |
+
# Here you can integrate an API for background removal, e.g., remove.bg or Hugging Face models.
|
| 39 |
+
# For now, it's a placeholder.
|
| 40 |
+
return image # Placeholder for background removal
|
| 41 |
+
|
| 42 |
+
# Main function
|
| 43 |
+
def main():
|
| 44 |
+
st.title("Advanced Image Editor")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 46 |
+
st.sidebar.title("Image Editor Tools")
|
| 47 |
+
uploaded_file = st.sidebar.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
|
| 48 |
|
| 49 |
+
if uploaded_file is not None:
|
| 50 |
+
image = Image.open(uploaded_file)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
|
| 53 |
|
| 54 |
+
st.sidebar.subheader("Enhance Image")
|
| 55 |
+
enhancement_type = st.sidebar.selectbox("Enhancement Type", ["Brightness", "Contrast", "Sharpness", "Color"])
|
| 56 |
+
enhancement_factor = st.sidebar.slider("Enhancement Factor", 0.5, 2.0, 1.0, 0.1)
|
| 57 |
|
| 58 |
+
enhanced_image = enhance_image(image, enhancement_type, enhancement_factor)
|
| 59 |
+
st.sidebar.subheader("Apply Filter")
|
| 60 |
+
filter_type = st.sidebar.selectbox("Select Filter", ["None", "Grayscale", "Blur"])
|
| 61 |
+
filtered_image = apply_filter(enhanced_image, filter_type)
|
| 62 |
|
| 63 |
+
st.image(filtered_image, caption="Processed Image", use_column_width=True)
|
|
|
|
| 64 |
|
| 65 |
+
# Background Removal
|
| 66 |
+
remove_bg = st.sidebar.checkbox("Remove Background")
|
| 67 |
+
if remove_bg:
|
| 68 |
+
filtered_image = remove_background(filtered_image)
|
| 69 |
+
st.image(filtered_image, caption="Background Removed Image", use_column_width=True)
|
| 70 |
|
| 71 |
+
# Download Button
|
| 72 |
+
buf = io.BytesIO()
|
| 73 |
+
filtered_image.save(buf, format="PNG")
|
| 74 |
+
byte_im = buf.getvalue()
|
| 75 |
+
st.sidebar.download_button("Download Edited Image", byte_im, "edited_image.png", "image/png")
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
main()
|