Spaces:
Runtime error
Runtime error
File size: 1,874 Bytes
726db91 b834bb8 726db91 |
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 |
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")
|