File size: 2,205 Bytes
f62fbb3
 
 
 
 
 
346e108
 
 
f62fbb3
 
 
 
 
9ed4995
f62fbb3
 
38c4bbd
 
9ed4995
38c4bbd
9ed4995
38c4bbd
 
f62fbb3
38c4bbd
346e108
 
 
38c4bbd
f62fbb3
346e108
 
38c4bbd
346e108
 
38c4bbd
346e108
 
 
 
 
9ed4995
346e108
 
9ed4995
346e108
 
 
9ed4995
346e108
 
 
38c4bbd
f62fbb3
 
 
346e108
38c4bbd
f62fbb3
9ed4995
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
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io

st.set_page_config(page_title="Colorize Image", page_icon="🎨", layout="centered")
st.title("🎨 Colorize Your Image")
st.markdown("Upload an image and apply a color tint dynamically!")

# Upload Image
uploaded_file = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg", "webp"])

if uploaded_file is not None:
    # Convert image to OpenCV format
    image = Image.open(uploaded_file)
    image = np.array(image)

    # Ensure the image has 3 channels (RGB)
    if len(image.shape) == 2:  # Convert grayscale to RGB
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    elif image.shape[-1] == 4:  # Convert RGBA to RGB
        image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)

    st.image(image, caption="Original Image", use_column_width=True)

    # Select Tint Color
    st.subheader("Select Tint Color")
    tint_color = st.color_picker("Pick a tint color", "#ff0000")  # Default Red

    # Convert Hex to RGB
    hex_color = tint_color.lstrip("#")
    tint_rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))

    # Intensity Slider
    intensity = st.slider("Tint Intensity", 0, 100, 50)

    # Colorize Function
    def apply_tint(img, color, intensity):
        # Convert to grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)  # Convert back to 3 channels

        # Create color overlay
        color_overlay = np.full_like(gray, color, dtype=np.uint8)

        # Blend grayscale with color overlay
        blended = cv2.addWeighted(gray, 1 - (intensity / 100), color_overlay, intensity / 100, 0)
        return blended

    # Apply Tint
    modified_image = apply_tint(image, tint_rgb, intensity)
    st.image(modified_image, caption="Tinted Image", use_column_width=True)

    # Download Button
    img_byte_arr = io.BytesIO()
    Image.fromarray(modified_image).save(img_byte_arr, format="PNG")
    st.download_button("Download Tinted Image", img_byte_arr.getvalue(), file_name="tinted_image.png", mime="image/png")

st.markdown("---")
st.markdown("💡 Developed with ❤️ using Streamlit, OpenCV, and NumPy")