File size: 2,691 Bytes
11b0939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"
    )