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")