mallelapreethi commited on
Commit
b52ce7d
·
verified ·
1 Parent(s): eabe443

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+ import io
6
+
7
+ def apply_grabcut(image):
8
+ # Create a mask
9
+ mask = np.zeros(image.shape[:2], np.uint8)
10
+
11
+ # Model for background and foreground
12
+ bgd_model = np.zeros((1, 65), np.float64)
13
+ fgd_model = np.zeros((1, 65), np.float64)
14
+
15
+ # Define a rectangle around the foreground object
16
+ rect = (10, 10, image.shape[1]-10, image.shape[0]-10)
17
+
18
+ # Apply GrabCut algorithm
19
+ cv2.grabCut(image, mask, rect, bgd_model, fgd_model, 5, cv2.GC_INIT_WITH_RECT)
20
+
21
+ # Create a mask where sure and likely foreground are 1, and background are 0
22
+ mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
23
+
24
+ # Separate the foreground and background
25
+ foreground = image * mask2[:, :, np.newaxis]
26
+ background = cv2.GaussianBlur(image, (31, 31), 0) * (1 - mask2[:, :, np.newaxis])
27
+
28
+ return foreground + background
29
+
30
+ # Function to convert image to sketch with adjustable outline thickness
31
+ def image_to_sketch(image, kernel_size=3):
32
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
33
+ inverted_image = 255 - gray_image
34
+ blurred_image = cv2.GaussianBlur(inverted_image, (31, 31), 0)
35
+ inverted_blurred = 255 - blurred_image
36
+ sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
37
+
38
+ # Apply adaptive thresholding to enhance edges
39
+ adaptive_thresh = cv2.adaptiveThreshold(sketch, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 2)
40
+
41
+ # Apply morphological operation to thicken the outlines
42
+ kernel = np.ones((kernel_size, kernel_size), np.uint8)
43
+ sketch = cv2.dilate(adaptive_thresh, kernel, iterations=1)
44
+
45
+ return sketch
46
+
47
+ # Streamlit app layout
48
+ st.set_page_config(page_title="Image to Sketch Converter", page_icon="🎨", layout="centered")
49
+
50
+ # Custom CSS for heading color and footer positioning
51
+ st.markdown("""
52
+ <style>
53
+ .title {
54
+ color: blue;
55
+ font-size: 2.5em;
56
+ font-weight: bold;
57
+ text-align: center;
58
+ }
59
+ .footer {
60
+ position: relative;
61
+ bottom: 0;
62
+ width: 100%;
63
+ background-color: orange;
64
+ text-align: center;
65
+ text-color: black;
66
+ padding: 10px;
67
+ font-weight: bold;
68
+ margin-top: 50px;
69
+ }
70
+ .content {
71
+ margin-bottom: 70px;
72
+ }
73
+ .spacing {
74
+ margin: 10px 10px;
75
+ }
76
+ .centered-button {
77
+ display: flex;
78
+ justify-content: center;
79
+ align-items: center;
80
+ gap: 10px;
81
+ }
82
+ </style>
83
+ """, unsafe_allow_html=True)
84
+
85
+ # Title and description
86
+ st.markdown('<p class="title">🎨 Image to Sketch Converter</p>', unsafe_allow_html=True)
87
+ st.markdown("""
88
+ Convert your images into beautiful sketches with this simple app.
89
+ Upload an image, and get the sketch version instantly! You can even download the sketch.
90
+ """)
91
+
92
+ # Example conversions
93
+ st.subheader("Example Conversions")
94
+
95
+ # Load and display example image
96
+ example_image_path = 'Dog.jpg'
97
+ example_image = cv2.imread(example_image_path)
98
+
99
+ if example_image is not None:
100
+ # Convert BGR to RGB for correct color display
101
+ example_image_rgb = cv2.cvtColor(example_image, cv2.COLOR_BGR2RGB)
102
+ example_blurred_background = apply_grabcut(example_image)
103
+ example_sketch = image_to_sketch(example_blurred_background)
104
+
105
+ col1, col2 = st.columns(2)
106
+
107
+ with col1:
108
+ st.image(example_image_rgb, caption='Original Image', use_column_width=True)
109
+ with col2:
110
+ st.image(example_sketch, caption='Sketch Image', use_column_width=True)
111
+ else:
112
+ st.error(f"Failed to load example image from path: {example_image_path}")
113
+
114
+ # User upload section
115
+ st.subheader("Upload Your Image")
116
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
117
+
118
+ if uploaded_file is not None:
119
+ # Load the image
120
+ image = Image.open(uploaded_file)
121
+ image_np = np.array(image)
122
+
123
+ # Determine the format of the uploaded image
124
+ image_format = image.format.lower()
125
+
126
+ st.write("Converting...")
127
+
128
+ # Kernel size slider for adjusting outline thickness
129
+ kernel_size = st.slider('Adjust Outline Thickness', 1, 10, 3)
130
+
131
+ # Apply GrabCut to blur background
132
+ blurred_background_image = apply_grabcut(image_np)
133
+
134
+ # Convert the image with blurred background to a sketch with the selected kernel size
135
+ sketch = image_to_sketch(blurred_background_image, kernel_size)
136
+
137
+ col3, col4 = st.columns(2)
138
+
139
+ with col3:
140
+ st.image(image, caption='Uploaded Image', use_column_width=True)
141
+ with col4:
142
+ st.image(sketch, caption='Sketch', use_column_width=True)
143
+
144
+ # Add some space before the button
145
+ st.markdown('<div class="spacing"></div>', unsafe_allow_html=True)
146
+
147
+ # Convert the sketch to an image and save to an in-memory file object
148
+ sketch_image = Image.fromarray(sketch)
149
+ buf = io.BytesIO()
150
+ sketch_image.save(buf, format=image_format.upper())
151
+ byte_im = buf.getvalue()
152
+
153
+ # Provide a download link for the sketch image in the center
154
+ st.markdown('<div class="centered-button">', unsafe_allow_html=True)
155
+ btn = st.download_button(
156
+ label="Download Sketch",
157
+ data=byte_im,
158
+ file_name=f"sketch.{image_format}",
159
+ mime=f"image/{image_format}"
160
+ )
161
+ st.markdown('</div>', unsafe_allow_html=True)
162
+ else:
163
+ st.info("Please upload an image to convert.")
164
+
165
+ # Footer
166
+ st.markdown("""
167
+ <div class="footer">
168
+ Made by Mallela Preethi
169
+ </div>
170
+ """, unsafe_allow_html=True)