|
import streamlit as st |
|
import cv2 |
|
import numpy as np |
|
from PIL import Image |
|
import io |
|
|
|
|
|
def image_to_sketch(image): |
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
inverted_image = 255 - gray_image |
|
blurred_image = cv2.GaussianBlur(inverted_image, (21, 21), 0) |
|
inverted_blurred = 255 - blurred_image |
|
sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0) |
|
return sketch |
|
|
|
|
|
st.set_page_config(page_title="Image to Sketch Converter", page_icon="π¨", layout="centered") |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.title { |
|
color: blue; |
|
font-size: 2.5em; |
|
font-weight: bold; |
|
text-align: center; |
|
} |
|
.footer { |
|
position: relative; |
|
bottom: 0; |
|
width: 100%; |
|
background-color: orange; |
|
text-align: center; |
|
text-color: black; |
|
padding: 10px; |
|
font-weight: bold; |
|
margin-top: 50px; |
|
} |
|
.content { |
|
margin-bottom: 70px; |
|
} |
|
.spacing { |
|
margin: 10px 10px; |
|
} |
|
.centered-button { |
|
display: flex; |
|
justify-content: center; |
|
align-items: center; |
|
gap: 10px; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown('<p class="title">π¨ Image to Sketch Converter</p>', unsafe_allow_html=True) |
|
st.markdown(""" |
|
Convert your images into beautiful sketches with this simple app. |
|
Upload an image, and get the sketch version instantly! You can even download the sketch. |
|
""") |
|
|
|
|
|
st.subheader("Example Conversions") |
|
|
|
|
|
example_image_path = 'Dog.jpg' |
|
example_image = cv2.imread(example_image_path) |
|
|
|
if example_image is not None: |
|
|
|
example_image_rgb = cv2.cvtColor(example_image, cv2.COLOR_BGR2RGB) |
|
example_sketch = image_to_sketch(example_image) |
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.image(example_image_rgb, caption='Original Image', use_column_width=True) |
|
with col2: |
|
st.image(example_sketch, caption='Sketch', use_column_width=True) |
|
else: |
|
st.error(f"Failed to load example image from path: {example_image_path}") |
|
|
|
|
|
st.subheader("Upload Your Image") |
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
image_np = np.array(image) |
|
|
|
|
|
image_format = image.format.lower() |
|
|
|
st.write("Converting...") |
|
|
|
|
|
|
|
sketch = image_to_sketch(image_np) |
|
|
|
col3, col4 = st.columns(2) |
|
|
|
with col3: |
|
st.image(image, caption='Uploaded Image', use_column_width=True) |
|
with col4: |
|
st.image(sketch, caption='Sketch', use_column_width=True) |
|
|
|
|
|
st.markdown('<div class="spacing"></div>', unsafe_allow_html=True) |
|
|
|
|
|
sketch_image = Image.fromarray(sketch) |
|
buf = io.BytesIO() |
|
sketch_image.save(buf, format=image_format.upper()) |
|
byte_im = buf.getvalue() |
|
|
|
|
|
st.markdown('<div class="centered-button">', unsafe_allow_html=True) |
|
btn = st.download_button( |
|
label="Download Sketch", |
|
data=byte_im, |
|
file_name=f"sketch.{image_format}", |
|
mime=f"image/{image_format}" |
|
) |
|
st.markdown('</div>', unsafe_allow_html=True) |
|
else: |
|
st.info("Please upload an image to convert.") |
|
|
|
|
|
st.markdown(""" |
|
<div class="footer"> |
|
Made by Mallela Preethi |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|