Image-2-SVG / app.py
pralayasimha's picture
Update app.py
b834bb8 verified
raw
history blame contribute delete
No virus
1.87 kB
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io
title_and_description = """
# PDF to Word and Word to PDF converter
Created by [@pralayasimha](https://pralayasimha.in)
"""
# Function to convert PIL image to OpenCV format
def pil_to_cv(image):
return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# Function to process the image and return edges
def process_image(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred_image = cv2.GaussianBlur(gray_image, (3, 3), 0)
edges = cv2.Canny(blurred_image, threshold1=30, threshold2=100)
return edges
# Function to convert edges to a simple SVG - this is a very basic example
def edges_to_svg(edges):
# This is a placeholder for a real conversion process
svg_data = "<svg width='100%' height='100%' xmlns='http://www.w3.org/2000/svg'>"
# Simple example: draw a line for each edge point (not efficient or accurate)
for y in range(edges.shape[0]):
for x in range(edges.shape[1]):
if edges[y, x] != 0: # If edge is detected
svg_data += f"<circle cx='{x}' cy='{y}' r='0.5' fill='black' />"
svg_data += "</svg>"
return svg_data
# Streamlit UI
st.title('Image to SVG Converter')
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
st.write("Processing...")
cv_image = pil_to_cv(image)
edges = process_image(cv_image)
st.image(edges, caption='Edge Detection Result', use_column_width=True)
svg_result = edges_to_svg(edges)
st.download_button(label="Download SVG",
data=svg_result,
file_name="result.svg",
mime="image/svg+xml")