Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image, ImageEnhance
|
3 |
+
import numpy as np
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
# Function to apply the filter
|
7 |
+
def apply_filter(img):
|
8 |
+
# Convert image to editable format
|
9 |
+
img = img.convert("RGB")
|
10 |
+
|
11 |
+
# Apply exposure
|
12 |
+
exposure = 1 + (-0.31) # Adjusted for Brightness
|
13 |
+
enhancer = ImageEnhance.Brightness(img)
|
14 |
+
img = enhancer.enhance(exposure)
|
15 |
+
|
16 |
+
# Apply contrast
|
17 |
+
contrast = 1 + (-18 / 100) # Adjusted for Contrast
|
18 |
+
enhancer = ImageEnhance.Contrast(img)
|
19 |
+
img = enhancer.enhance(contrast)
|
20 |
+
|
21 |
+
# Highlights and shadows approximation
|
22 |
+
img_array = np.array(img, dtype=np.float32) / 255.0
|
23 |
+
shadows = 1.5 # Adjusted for Shadows (+50)
|
24 |
+
highlights = 0.0 # Adjusted for Highlights (-100)
|
25 |
+
img_array = np.clip(img_array * shadows + highlights, 0, 1)
|
26 |
+
img = Image.fromarray((img_array * 255).astype(np.uint8))
|
27 |
+
|
28 |
+
# Whites and blacks approximation
|
29 |
+
whites = 1.12 # Adjusted for Whites (+12)
|
30 |
+
blacks = 0.80 # Adjusted for Blacks (-20)
|
31 |
+
img_array = np.clip(img_array * whites * blacks, 0, 1)
|
32 |
+
img = Image.fromarray((img_array * 255).astype(np.uint8))
|
33 |
+
|
34 |
+
# Vibrance and saturation
|
35 |
+
img_hsv = img.convert("HSV")
|
36 |
+
h, s, v = img_hsv.split()
|
37 |
+
vibrance = 1 + 70 / 100
|
38 |
+
s = s.point(lambda i: min(255, max(0, int(i * vibrance))))
|
39 |
+
saturation = 1 + 20 / 100
|
40 |
+
s = s.point(lambda i: min(255, max(0, int(i * saturation))))
|
41 |
+
img = Image.merge("HSV", (h, s, v)).convert("RGB")
|
42 |
+
|
43 |
+
# Texture and sharpening approximation
|
44 |
+
texture = 1 + 4 / 100
|
45 |
+
sharpening = 1 + 18 / 100
|
46 |
+
enhancer = ImageEnhance.Sharpness(img)
|
47 |
+
img = enhancer.enhance(texture * sharpening)
|
48 |
+
|
49 |
+
# Radius and detail (approximated using sharpening and clarity)
|
50 |
+
radius = 1.2
|
51 |
+
detail = 1 + 30 / 100
|
52 |
+
img = img.filter(ImageEnhance.UnsharpMask(radius=radius, percent=detail * 100, threshold=3))
|
53 |
+
|
54 |
+
return img
|
55 |
+
|
56 |
+
# Streamlit app
|
57 |
+
st.title("Classic Photo Editor")
|
58 |
+
st.write("Upload an image to apply the filter.")
|
59 |
+
|
60 |
+
# Upload image
|
61 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
62 |
+
|
63 |
+
if uploaded_file:
|
64 |
+
# Open the uploaded image
|
65 |
+
img = Image.open(uploaded_file)
|
66 |
+
|
67 |
+
# Display the original image
|
68 |
+
st.subheader("Original Image")
|
69 |
+
st.image(img, use_column_width=True)
|
70 |
+
|
71 |
+
# Apply the filter
|
72 |
+
st.subheader("Filtered Image")
|
73 |
+
filtered_img = apply_filter(img)
|
74 |
+
st.image(filtered_img, use_column_width=True)
|
75 |
+
|
76 |
+
# Provide download button
|
77 |
+
buf = BytesIO()
|
78 |
+
filtered_img.save(buf, format="JPEG")
|
79 |
+
byte_im = buf.getvalue()
|
80 |
+
st.download_button(
|
81 |
+
label="Download Filtered Image",
|
82 |
+
data=byte_im,
|
83 |
+
file_name="filtered_image.jpg",
|
84 |
+
mime="image/jpeg"
|
85 |
+
)
|