Py.Cafe

ARAVINDGIT96/

interactive-brightness-contrast-adjuster

🖼️ Interactive Brightness/Contrast Adjuster

DocsPricing
  • app.py
  • requirements.txt
app.py
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
import streamlit as st
import cv2
import numpy as np
from PIL import Image

st.set_page_config(page_title="Interactive Brightness/Contrast Adjuster")

st.title("🖼️ Interactive Brightness/Contrast Adjuster")
st.write("Upload an image and use the sliders to adjust brightness and contrast in real time.")

# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png", "bmp"])

def adjust_brightness_contrast(img, brightness=0, contrast=0):
    """Adjust brightness and contrast using OpenCV."""
    adjusted = cv2.convertScaleAbs(img, alpha=1 + contrast/100, beta=brightness)
    return adjusted

if uploaded_file:
    # Convert uploaded file to an OpenCV image
    image = Image.open(uploaded_file).convert('RGB')
    img_np = np.array(image)
    img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)

    # Sliders
    brightness = st.slider("Brightness", -100, 100, 0)
    contrast = st.slider("Contrast", -100, 100, 0)

    # Adjust the image
    adjusted_img = adjust_brightness_contrast(img_cv, brightness, contrast)

    # Convert back to RGB for display
    adjusted_rgb = cv2.cvtColor(adjusted_img, cv2.COLOR_BGR2RGB)

    # Display original and adjusted side by side
    col1, col2 = st.columns(2)
    with col1:
        st.image(image, caption="Original", use_column_width=True)
    with col2:
        st.image(adjusted_rgb, caption="Adjusted", use_column_width=True)
else:
    st.info("Please upload an image to get started.")