subhanliaqat
commited on
Commit
•
0b49bdd
1
Parent(s):
5c97577
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
def convertto_watercolorsketch(inp_img):
|
8 |
+
img_1 = cv2.edgePreservingFilter(inp_img, flags=2, sigma_s=50, sigma_r=0.8)
|
9 |
+
img_water_color = cv2.stylization(img_1, sigma_s=100, sigma_r=0.5)
|
10 |
+
return(img_water_color)
|
11 |
+
|
12 |
+
def pencilsketch(inp_img):
|
13 |
+
img_pencil_sketch, pencil_color_sketch = cv2.pencilSketch(
|
14 |
+
inp_img, sigma_s=50, sigma_r=0.07, shade_factor=0.0825)
|
15 |
+
return(img_pencil_sketch)
|
16 |
+
|
17 |
+
def load_an_image(image):
|
18 |
+
img = Image.open(image)
|
19 |
+
return img
|
20 |
+
|
21 |
+
def main():
|
22 |
+
st.title('WEB APPLICATION TO CONVERT IMAGE TO SKETCH')
|
23 |
+
st.write("This is an application developed for converting " +
|
24 |
+
"your ***image*** to a ***Water Color Sketch*** OR ***Pencil Sketch***")
|
25 |
+
st.subheader("Please Upload your image")
|
26 |
+
|
27 |
+
image_file = st.file_uploader("Upload Images", type=["png", "jpg", "jpeg"])
|
28 |
+
|
29 |
+
if image_file is not None:
|
30 |
+
option = st.selectbox('How would you like to convert the image',
|
31 |
+
('Convert to water color sketch',
|
32 |
+
'Convert to pencil sketch'))
|
33 |
+
if option == 'Convert to water color sketch':
|
34 |
+
image = Image.open(image_file)
|
35 |
+
final_sketch = convertto_watercolorsketch(np.array(image))
|
36 |
+
im_pil = Image.fromarray(final_sketch)
|
37 |
+
col1, col2 = st.columns(2)
|
38 |
+
|
39 |
+
with col1:
|
40 |
+
st.header("Original Image")
|
41 |
+
st.image(load_an_image(image_file), width=250)
|
42 |
+
|
43 |
+
with col2:
|
44 |
+
st.header("Water Color Sketch")
|
45 |
+
st.image(im_pil, width=250)
|
46 |
+
buf = BytesIO()
|
47 |
+
img = im_pil
|
48 |
+
img.save(buf, format="JPEG")
|
49 |
+
byte_im = buf.getvalue()
|
50 |
+
st.download_button(
|
51 |
+
label="Download image",
|
52 |
+
data=byte_im,
|
53 |
+
file_name="watercolorsketch.png",
|
54 |
+
mime="image/png"
|
55 |
+
)
|
56 |
+
|
57 |
+
if option == 'Convert to pencil sketch':
|
58 |
+
image = Image.open(image_file)
|
59 |
+
final_sketch = pencilsketch(np.array(image))
|
60 |
+
im_pil = Image.fromarray(final_sketch)
|
61 |
+
col1, col2 = st.columns(2)
|
62 |
+
|
63 |
+
with col1:
|
64 |
+
st.header("Original Image")
|
65 |
+
st.image(load_an_image(image_file), width=250)
|
66 |
+
|
67 |
+
with col2:
|
68 |
+
st.header("Pencil Sketch")
|
69 |
+
st.image(im_pil, width=250)
|
70 |
+
buf = BytesIO()
|
71 |
+
img = im_pil
|
72 |
+
img.save(buf, format="JPEG")
|
73 |
+
byte_im = buf.getvalue()
|
74 |
+
st.download_button(
|
75 |
+
label="Download image",
|
76 |
+
data=byte_im,
|
77 |
+
file_name="watercolorsketch.png",
|
78 |
+
mime="image/png"
|
79 |
+
)
|
80 |
+
|
81 |
+
if __name__ == '__main__':
|
82 |
+
main()
|