Photo2Sketch / app.py
Dani786's picture
Update app.py
ff570bd verified
import streamlit as st
import cv2
import numpy as np
from PIL import Image
from io import BytesIO
st.set_page_config(page_title="Image to Sketch", layout="centered")
st.title("🖼️ Image to Sketch Converter")
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert("RGB")
img_array = np.array(image)
st.subheader("Original Image")
st.image(img_array, use_container_width=True)
# Convert to grayscale
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
# Invert the image
inv = 255 - gray
# Blur the image
blur = cv2.GaussianBlur(inv, (21, 21), 0)
# Create the sketch
sketch = cv2.divide(gray, 255 - blur, scale=256)
st.subheader("Sketch Output")
st.image(sketch, use_container_width=True, channels="GRAY")
# Convert sketch to PIL image
sketch_pil = Image.fromarray(sketch)
# Save image to memory
buf = BytesIO()
sketch_pil.save(buf, format="PNG")
byte_im = buf.getvalue()
# Download button
st.download_button(
label="📥 Download Sketch",
data=byte_im,
file_name="sketch.png",
mime="image/png"
)