aigorithm commited on
Commit
c060212
·
verified ·
1 Parent(s): 7fba65a
Files changed (1) hide show
  1. app.py +10 -34
app.py CHANGED
@@ -1,22 +1,21 @@
 
1
  import streamlit as st
2
  from PIL import Image
3
  import numpy as np
4
  import cv2
5
  import svgwrite
6
  import io
7
- import os
8
 
9
- st.set_page_config(page_title="Image to Embroidery", layout="centered")
10
- st.title("🧵 Image to Embroidery Converter")
11
- st.write("Upload an image and get both an SVG embroidery preview and a short video!")
12
 
13
- uploaded_file = st.file_uploader("📷 Upload an image", type=["jpg", "jpeg", "png"])
14
 
15
- # === Stitch Preview Function ===
16
  def process_image(uploaded_image):
17
  img = Image.open(uploaded_image).convert("L")
18
  img = img.resize((300, 300))
19
  img_np = np.array(img)
 
20
  edges = cv2.Canny(img_np, threshold1=100, threshold2=200)
21
 
22
  svg = svgwrite.Drawing(size=(img.width, img.height))
@@ -29,33 +28,10 @@ def process_image(uploaded_image):
29
  svg.write(svg_buffer)
30
  return svg_buffer.getvalue()
31
 
32
- # === Video Generator Function ===
33
- def create_video_from_image(image_file, duration=3, fps=10):
34
- img = Image.open(image_file).convert("RGB")
35
- img = img.resize((600, 600))
36
- frame = np.array(img)
37
- height, width, _ = frame.shape
38
-
39
- output_path = "embroidery_preview.mp4"
40
- total_frames = duration * fps
41
- out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
42
-
43
- for _ in range(total_frames):
44
- out.write(frame)
45
-
46
- out.release()
47
- return output_path
48
-
49
- # === App Logic ===
50
  if uploaded_file:
51
- st.image(uploaded_file, caption="🖼️ Uploaded Image", use_column_width=True)
52
-
53
- # SVG generation
54
  svg_data = process_image(uploaded_file)
55
- st.download_button("⬇️ Download SVG", data=svg_data, file_name="embroidery_preview.svg", mime="image/svg+xml")
56
- st.code(svg_data, language="xml")
57
-
58
- # Video generation
59
- video_path = create_video_from_image(uploaded_file)
60
- with open(video_path, "rb") as video_file:
61
- st.download_button("🎥 Download MP4 Video", data=video_file, file_name="embroidery_preview.mp4", mime="video/mp4")
 
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))
 
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")