mallelapreethi commited on
Commit
f5fcd03
·
verified ·
1 Parent(s): b717ae9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -2,12 +2,13 @@ import streamlit as st
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
  # Function to convert image to sketch
7
  def image_to_sketch(image):
8
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
  inverted_image = 255 - gray_image
10
- blurred_image = cv2.GaussianBlur(inverted_image, (21, 21), 0)
11
  inverted_blurred = 255 - blurred_image
12
  sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
13
  return sketch
@@ -37,12 +38,13 @@ st.markdown("""
37
  margin-bottom: 70px;
38
  }
39
  .spacing {
40
- margin: 10px 0;
41
  }
42
  .centered-button {
43
  display: flex;
44
  justify-content: center;
45
  align-items: center;
 
46
  }
47
  </style>
48
  """, unsafe_allow_html=True)
@@ -51,7 +53,7 @@ st.markdown("""
51
  st.markdown('<p class="title">🎨 Image to Sketch Converter</p>', unsafe_allow_html=True)
52
  st.markdown("""
53
  Convert your images into beautiful sketches with this simple app.
54
- Upload an image, and get the sketch version instantly!
55
  """)
56
 
57
  # Example conversions
@@ -80,11 +82,17 @@ st.subheader("Upload Your Image")
80
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
81
 
82
  if uploaded_file is not None:
83
- image = np.array(Image.open(uploaded_file))
 
 
 
 
 
84
 
85
  st.write("Converting...")
86
 
87
- sketch = image_to_sketch(image)
 
88
 
89
  col3, col4 = st.columns(2)
90
 
@@ -96,13 +104,19 @@ if uploaded_file is not None:
96
  # Add some space before the button
97
  st.markdown('<div class="spacing"></div>', unsafe_allow_html=True)
98
 
 
 
 
 
 
 
99
  # Provide a download link for the sketch image in the center
100
  st.markdown('<div class="centered-button">', unsafe_allow_html=True)
101
  btn = st.download_button(
102
  label="Download Sketch",
103
- data=Image.fromarray(sketch).tobytes(),
104
- file_name="sketch.png",
105
- mime="image/png"
106
  )
107
  st.markdown('</div>', unsafe_allow_html=True)
108
  else:
 
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
5
+ import io
6
 
7
  # Function to convert image to sketch
8
  def image_to_sketch(image):
9
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10
  inverted_image = 255 - gray_image
11
+ blurred_image = cv2.GaussianBlur(inverted_image, (31, 31), 0)
12
  inverted_blurred = 255 - blurred_image
13
  sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
14
  return sketch
 
38
  margin-bottom: 70px;
39
  }
40
  .spacing {
41
+ margin: 10px 10px;
42
  }
43
  .centered-button {
44
  display: flex;
45
  justify-content: center;
46
  align-items: center;
47
+ gap: 10px;
48
  }
49
  </style>
50
  """, unsafe_allow_html=True)
 
53
  st.markdown('<p class="title">🎨 Image to Sketch Converter</p>', unsafe_allow_html=True)
54
  st.markdown("""
55
  Convert your images into beautiful sketches with this simple app.
56
+ Upload an image, and get the sketch version instantly! You can even download the sketch.
57
  """)
58
 
59
  # Example conversions
 
82
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
83
 
84
  if uploaded_file is not None:
85
+ # Load the image
86
+ image = Image.open(uploaded_file)
87
+ image_np = np.array(image)
88
+
89
+ # Determine the format of the uploaded image
90
+ image_format = image.format.lower()
91
 
92
  st.write("Converting...")
93
 
94
+ # Convert the image to a sketch
95
+ sketch = image_to_sketch(image_np)
96
 
97
  col3, col4 = st.columns(2)
98
 
 
104
  # Add some space before the button
105
  st.markdown('<div class="spacing"></div>', unsafe_allow_html=True)
106
 
107
+ # Convert the sketch to an image and save to an in-memory file object
108
+ sketch_image = Image.fromarray(sketch)
109
+ buf = io.BytesIO()
110
+ sketch_image.save(buf, format=image_format.upper())
111
+ byte_im = buf.getvalue()
112
+
113
  # Provide a download link for the sketch image in the center
114
  st.markdown('<div class="centered-button">', unsafe_allow_html=True)
115
  btn = st.download_button(
116
  label="Download Sketch",
117
+ data=byte_im,
118
+ file_name=f"sketch.{image_format}",
119
+ mime=f"image/{image_format}"
120
  )
121
  st.markdown('</div>', unsafe_allow_html=True)
122
  else: