-
Notifications
You must be signed in to change notification settings - Fork 3
/
sketch.py
56 lines (43 loc) · 1.68 KB
/
sketch.py
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
import streamlit as st
import numpy as np
from PIL import Image
import cv2
import base64
from io import BytesIO
def get_image_download_link(img):
"""Generates a link allowing the PIL image to be downloaded
in: PIL image
out: href string
"""
buffered = BytesIO()
img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode()
href = f'<a href="data:file/jpg;base64,{img_str}">Download Your Converted Image </a>'
return href
def dodgeV2(x, y):
return cv2.divide(x, 255 - y, scale=256)
def pencilsketch(inp_img):
img_gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
img_invert = cv2.bitwise_not(img_gray)
img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)
final_img = dodgeV2(img_gray, img_smoothing)
return(final_img)
def main():
st.title("Image Filter App")
st.write("Streamlit App to convert your photos to realistic Pencil Sketches")
file_image = st.sidebar.file_uploader("Upload your Photos", type=['jpeg','jpg','png'])
option = st.sidebar.selectbox( 'How would your your images to be converted ?',
('pencilsketch', 'pencilsketch', 'pencilsketch'))
if file_image is None:
st.write("You haven't uploaded any image file")
else:
input_img = Image.open(file_image)
final_sketch = pencilsketch(np.array(input_img))
st.write("**Input Photo**")
st.image(input_img, use_column_width=True)
st.write("**Output Pencil Sketch**")
st.image(final_sketch, use_column_width=True)
result = Image.fromarray(final_sketch)
st.markdown(get_image_download_link(result), unsafe_allow_html=True)
if __name__ == '__main__':
main()