File size: 2,374 Bytes
0b49bdd |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import streamlit as st
from PIL import Image
from io import BytesIO
import numpy as np
import cv2
def convertto_watercolorsketch(inp_img):
img_1 = cv2.edgePreservingFilter(inp_img, flags=2, sigma_s=50, sigma_r=0.8)
img_water_color = cv2.stylization(img_1, sigma_s=100, sigma_r=0.5)
return(img_water_color)
def pencilsketch(inp_img):
img_pencil_sketch, pencil_color_sketch = cv2.pencilSketch(
inp_img, sigma_s=50, sigma_r=0.07, shade_factor=0.0825)
return(img_pencil_sketch)
def load_an_image(image):
img = Image.open(image)
return img
def main():
st.title('WEB APPLICATION TO CONVERT IMAGE TO SKETCH')
st.write("This is an application developed for converting " +
"your ***image*** to a ***Water Color Sketch*** OR ***Pencil Sketch***")
st.subheader("Please Upload your image")
image_file = st.file_uploader("Upload Images", type=["png", "jpg", "jpeg"])
if image_file is not None:
option = st.selectbox('How would you like to convert the image',
('Convert to water color sketch',
'Convert to pencil sketch'))
if option == 'Convert to water color sketch':
image = Image.open(image_file)
final_sketch = convertto_watercolorsketch(np.array(image))
im_pil = Image.fromarray(final_sketch)
col1, col2 = st.columns(2)
with col1:
st.header("Original Image")
st.image(load_an_image(image_file), width=250)
with col2:
st.header("Water Color Sketch")
st.image(im_pil, width=250)
buf = BytesIO()
img = im_pil
img.save(buf, format="JPEG")
byte_im = buf.getvalue()
st.download_button(
label="Download image",
data=byte_im,
file_name="watercolorsketch.png",
mime="image/png"
)
if option == 'Convert to pencil sketch':
image = Image.open(image_file)
final_sketch = pencilsketch(np.array(image))
im_pil = Image.fromarray(final_sketch)
col1, col2 = st.columns(2)
with col1:
st.header("Original Image")
st.image(load_an_image(image_file), width=250)
with col2:
st.header("Pencil Sketch")
st.image(im_pil, width=250)
buf = BytesIO()
img = im_pil
img.save(buf, format="JPEG")
byte_im = buf.getvalue()
st.download_button(
label="Download image",
data=byte_im,
file_name="watercolorsketch.png",
mime="image/png"
)
if __name__ == '__main__':
main() |