Spaces:
Sleeping
Sleeping
upload 2 files
Browse files- app.py +37 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import svgwrite
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
st.title("Image to Embroidery Converter")
|
| 10 |
+
st.write("Upload an image and get a stitch-style SVG preview.")
|
| 11 |
+
|
| 12 |
+
uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 13 |
+
|
| 14 |
+
def process_image(uploaded_image):
|
| 15 |
+
img = Image.open(uploaded_image).convert("L")
|
| 16 |
+
img = img.resize((300, 300))
|
| 17 |
+
img_np = np.array(img)
|
| 18 |
+
|
| 19 |
+
edges = cv2.Canny(img_np, threshold1=100, threshold2=200)
|
| 20 |
+
|
| 21 |
+
svg = svgwrite.Drawing(size=(img.width, img.height))
|
| 22 |
+
for y in range(1, edges.shape[0] - 1):
|
| 23 |
+
for x in range(1, edges.shape[1] - 1):
|
| 24 |
+
if edges[y, x] == 255:
|
| 25 |
+
svg.add(svg.circle(center=(x, y), r=0.4, fill='black'))
|
| 26 |
+
|
| 27 |
+
svg_buffer = io.StringIO()
|
| 28 |
+
svg.write(svg_buffer)
|
| 29 |
+
return svg_buffer.getvalue()
|
| 30 |
+
|
| 31 |
+
if uploaded_file:
|
| 32 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 33 |
+
svg_data = process_image(uploaded_file)
|
| 34 |
+
|
| 35 |
+
st.download_button("Download SVG", data=svg_data, file_name="embroidery_preview.svg", mime="image/svg+xml")
|
| 36 |
+
st.markdown("### Embroidery Stitch Preview (SVG)")
|
| 37 |
+
st.code(svg_data, language="xml")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
Pillow
|
| 5 |
+
svgwrite
|