File size: 1,230 Bytes
31f744d
 
 
 
ff570bd
31f744d
 
 
 
 
 
 
 
 
 
 
b6fd11b
31f744d
 
 
 
 
 
 
 
 
 
 
c1d80b6
 
ff570bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"
    )