Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Function to convert PIL image to OpenCV format
|
8 |
+
def pil_to_cv(image):
|
9 |
+
return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
10 |
+
|
11 |
+
# Function to process the image and return edges
|
12 |
+
def process_image(image):
|
13 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
14 |
+
blurred_image = cv2.GaussianBlur(gray_image, (3, 3), 0)
|
15 |
+
edges = cv2.Canny(blurred_image, threshold1=30, threshold2=100)
|
16 |
+
return edges
|
17 |
+
|
18 |
+
# Function to convert edges to a simple SVG - this is a very basic example
|
19 |
+
def edges_to_svg(edges):
|
20 |
+
# This is a placeholder for a real conversion process
|
21 |
+
svg_data = "<svg width='100%' height='100%' xmlns='http://www.w3.org/2000/svg'>"
|
22 |
+
# Simple example: draw a line for each edge point (not efficient or accurate)
|
23 |
+
for y in range(edges.shape[0]):
|
24 |
+
for x in range(edges.shape[1]):
|
25 |
+
if edges[y, x] != 0: # If edge is detected
|
26 |
+
svg_data += f"<circle cx='{x}' cy='{y}' r='0.5' fill='black' />"
|
27 |
+
svg_data += "</svg>"
|
28 |
+
return svg_data
|
29 |
+
|
30 |
+
# Streamlit UI
|
31 |
+
st.title('Image to SVG Converter')
|
32 |
+
|
33 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
34 |
+
if uploaded_file is not None:
|
35 |
+
image = Image.open(uploaded_file)
|
36 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
37 |
+
st.write("Processing...")
|
38 |
+
|
39 |
+
cv_image = pil_to_cv(image)
|
40 |
+
edges = process_image(cv_image)
|
41 |
+
|
42 |
+
st.image(edges, caption='Edge Detection Result', use_column_width=True)
|
43 |
+
|
44 |
+
svg_result = edges_to_svg(edges)
|
45 |
+
st.download_button(label="Download SVG",
|
46 |
+
data=svg_result,
|
47 |
+
file_name="result.svg",
|
48 |
+
mime="image/svg+xml")
|
49 |
+
|