-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketchbook.py
62 lines (46 loc) · 1.64 KB
/
sketchbook.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
57
58
59
60
61
62
from fileinput import filename
import cv2 as cv
import numpy as np
import random
from edges import Edges
from background import Background
from PIL import Image, ImageOps
import streamlit as st
class Sketch:
def __init__(self, image):
self.img = cv.imread(image)
def sketch(self):
grey = cv.cvtColor(self.img, cv.COLOR_BGR2GRAY)
inv = 255 - grey
blur = cv.GaussianBlur(inv, (13, 13), 0)
return cv.divide(grey, 255-blur, scale=256)
st.set_page_config(page_title='Sketcher',
page_icon="✏️", layout='wide', initial_sidebar_state='auto')
st.header("Sketcher")
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
filename = st.file_uploader("Choose a image file", type="jpg")
img = Image.open(filename)
sketch = Sketch(filename).sketch()
cv.imwrite("sketch.png", sketch)
bg = Background(img.size, octaves=6).background()
edges = Edges(filename).edges()
#sketchTrans = cv.cvtColor(sketch, cv.COLOR_GRAY2RGBA)
mask = edges[3]
sketch = cv.bitwise_and(sketch, edges, edges)
(thresh, sketch) = cv.threshold(sketch, 240, 255, cv.THRESH_BINARY)
#sketch = cv.multiply(sketch, np.array(bg), scale=(1./128))
h, w = sketch.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
#mask[1:h+1, 1:w+1] = sketch
sketchColor = cv.cvtColor(sketch, cv.COLOR_GRAY2RGBA)
white = np.all(sketchColor == [255, 255, 255, 255], axis=-1)
sketchColor[white, -1] = 0
cv.imwrite("final.png", sketchColor)
final = Image.fromarray(sketchColor)
final.show()