Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image, ImageEnhance | |
import numpy as np | |
from io import BytesIO | |
# Function to apply the filter | |
def apply_filter(img): | |
# Convert image to editable format | |
img = img.convert("RGB") | |
# Apply exposure | |
exposure = 1 + (-0.31) # Adjusted for Brightness | |
enhancer = ImageEnhance.Brightness(img) | |
img = enhancer.enhance(exposure) | |
# Apply contrast | |
contrast = 1 + (-18 / 100) # Adjusted for Contrast | |
enhancer = ImageEnhance.Contrast(img) | |
img = enhancer.enhance(contrast) | |
# Highlights and shadows approximation | |
img_array = np.array(img, dtype=np.float32) / 255.0 | |
shadows = 1.5 # Adjusted for Shadows (+50) | |
highlights = 0.0 # Adjusted for Highlights (-100) | |
img_array = np.clip(img_array * shadows + highlights, 0, 1) | |
img = Image.fromarray((img_array * 255).astype(np.uint8)) | |
# Whites and blacks approximation | |
whites = 1.12 # Adjusted for Whites (+12) | |
blacks = 0.80 # Adjusted for Blacks (-20) | |
img_array = np.clip(img_array * whites * blacks, 0, 1) | |
img = Image.fromarray((img_array * 255).astype(np.uint8)) | |
# Vibrance and saturation | |
img_hsv = img.convert("HSV") | |
h, s, v = img_hsv.split() | |
vibrance = 1 + 70 / 100 | |
s = s.point(lambda i: min(255, max(0, int(i * vibrance)))) | |
saturation = 1 + 20 / 100 | |
s = s.point(lambda i: min(255, max(0, int(i * saturation)))) | |
img = Image.merge("HSV", (h, s, v)).convert("RGB") | |
# Texture and sharpening approximation | |
texture = 1 + 4 / 100 | |
sharpening = 1 + 18 / 100 | |
enhancer = ImageEnhance.Sharpness(img) | |
img = enhancer.enhance(texture * sharpening) | |
# Radius and detail (approximated using sharpening and clarity) | |
radius = 1.2 | |
detail = 1 + 30 / 100 | |
img = img.filter(ImageEnhance.UnsharpMask(radius=radius, percent=detail * 100, threshold=3)) | |
return img | |
# Streamlit app | |
st.title("Classic Photo Editor") | |
st.write("Upload an image to apply the filter.") | |
# Upload image | |
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"]) | |
if uploaded_file: | |
# Open the uploaded image | |
img = Image.open(uploaded_file) | |
# Display the original image | |
st.subheader("Original Image") | |
st.image(img, use_column_width=True) | |
# Apply the filter | |
st.subheader("Filtered Image") | |
filtered_img = apply_filter(img) | |
st.image(filtered_img, use_column_width=True) | |
# Provide download button | |
buf = BytesIO() | |
filtered_img.save(buf, format="JPEG") | |
byte_im = buf.getvalue() | |
st.download_button( | |
label="Download Filtered Image", | |
data=byte_im, | |
file_name="filtered_image.jpg", | |
mime="image/jpeg" | |
) | |