Avi3738 commited on
Commit
1c46823
·
verified ·
1 Parent(s): 640c451

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -4,6 +4,8 @@ from skimage.color import rgb2gray
4
  from skimage.transform import resize
5
  from skimage.draw import line
6
  import gradio as gr
 
 
7
 
8
  def preprocess_image(image, size=(200, 200)):
9
  if image.ndim == 3:
@@ -28,7 +30,7 @@ def line_darkness_score(img, pin1, pin2):
28
  rr = np.clip(rr, 0, h-1)
29
  cc = np.clip(cc, 0, w-1)
30
  line_pixels = img[rr, cc]
31
- score = np.sum(1 - line_pixels) # invert because black is 0
32
  return score
33
 
34
  def generate_connections(img, pins, num_connections):
@@ -61,22 +63,23 @@ def draw_string_art(image, num_pins, num_connections):
61
  connections = generate_connections(img, pins, num_connections)
62
  # Draw the string art
63
  fig, ax = plt.subplots(figsize=(8, 8))
64
- ax.set_facecolor("black")
65
  ax.axis("off")
66
  for c in connections:
67
  p1, p2 = pins[c[0]], pins[c[1]]
68
- ax.plot([p1[0], p2[0]], [p1[1], p2[1]], 'w-', linewidth=0.3)
69
- ax.scatter(pins[:,0], pins[:,1], c='red', s=20)
 
70
  plt.close(fig)
71
  # Prepare connections file (1-based indexing)
72
- connections_1_based = [(c[0]+1, c[1]+1) for c in connections]
73
- flat = [str(pin) for conn in connections_1_based for pin in conn]
74
- connections_str = ', '.join(flat)
75
- # Save to file
76
- filename = "string_art_connections.txt"
77
- with open(filename, "w") as f:
78
- f.write(connections_str)
79
- return fig, filename
80
 
81
  def interface(image, num_pins, num_connections):
82
  fig, filename = draw_string_art(image, num_pins, num_connections)
@@ -94,7 +97,7 @@ demo = gr.Interface(
94
  gr.File(label="Connections File")
95
  ],
96
  title="String Art Generator",
97
- description="Upload an image, set number of pins and connections, and generate circular string art with downloadable connection file."
98
  )
99
 
100
  if __name__ == "__main__":
 
4
  from skimage.transform import resize
5
  from skimage.draw import line
6
  import gradio as gr
7
+ import tempfile
8
+ import os
9
 
10
  def preprocess_image(image, size=(200, 200)):
11
  if image.ndim == 3:
 
30
  rr = np.clip(rr, 0, h-1)
31
  cc = np.clip(cc, 0, w-1)
32
  line_pixels = img[rr, cc]
33
+ score = np.sum(1 - line_pixels) # black = 0, so 1 - pixel for darkness
34
  return score
35
 
36
  def generate_connections(img, pins, num_connections):
 
63
  connections = generate_connections(img, pins, num_connections)
64
  # Draw the string art
65
  fig, ax = plt.subplots(figsize=(8, 8))
66
+ ax.set_facecolor("white") # White background for visibility
67
  ax.axis("off")
68
  for c in connections:
69
  p1, p2 = pins[c[0]], pins[c[1]]
70
+ ax.plot([p1[0], p2[0]], [p1[1], p2[1]], 'k-', linewidth=0.7, alpha=0.9) # Black, thicker lines
71
+ ax.scatter(pins[:,0], pins[:,1], c='red', s=16) # Red pins, moderate size
72
+ plt.tight_layout(pad=0)
73
  plt.close(fig)
74
  # Prepare connections file (1-based indexing)
75
+ connections_1_based = [str(c[0]+1) for c in connections]
76
+ # Make continuous: start at pin 1, then next, etc.
77
+ connections_str = ', '.join(connections_1_based)
78
+ # Save to temporary file
79
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8")
80
+ temp_file.write(connections_str)
81
+ temp_file.close()
82
+ return fig, temp_file.name
83
 
84
  def interface(image, num_pins, num_connections):
85
  fig, filename = draw_string_art(image, num_pins, num_connections)
 
97
  gr.File(label="Connections File")
98
  ],
99
  title="String Art Generator",
100
+ description="Upload an image, set the number of pins and connections, and generate circular string art with a downloadable connection file."
101
  )
102
 
103
  if __name__ == "__main__":