prasannareddyp commited on
Commit
77e24f8
·
verified ·
1 Parent(s): 2dca1e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -15
app.py CHANGED
@@ -12,16 +12,15 @@ For batch processing, upload a .zip of images (PNG/JPG/JPEG) and download the ou
12
  """
13
 
14
  EXAMPLES_DIR = "examples"
 
15
 
16
- def _ensure_examples():
 
17
  os.makedirs(EXAMPLES_DIR, exist_ok=True)
18
- paths = [
19
- os.path.join(EXAMPLES_DIR, "checkerboard.png"),
20
- os.path.join(EXAMPLES_DIR, "gradient.png"),
21
- os.path.join(EXAMPLES_DIR, "shapes.png"),
22
- ]
23
  # 1) Checkerboard
24
- if not os.path.exists(paths[0]):
 
25
  cb = Image.new("RGB", (512, 512), "white")
26
  draw = ImageDraw.Draw(cb)
27
  tile = 64
@@ -29,18 +28,22 @@ def _ensure_examples():
29
  for x in range(0, 512, tile):
30
  if (x//tile + y//tile) % 2 == 0:
31
  draw.rectangle([x, y, x+tile-1, y+tile-1], fill=(30, 30, 30))
32
- cb.save(paths[0])
 
33
  # 2) Gradient
34
- if not os.path.exists(paths[1]):
 
35
  arr = np.zeros((360, 640, 3), dtype=np.uint8)
36
  for x in range(640):
37
  arr[:, x, 0] = int(255 * x / 639)
38
  for y in range(360):
39
  arr[y, :, 1] = int(255 * y / 359)
40
  arr[:, :, 2] = 160
41
- Image.fromarray(arr).save(paths[1])
 
42
  # 3) Shapes
43
- if not os.path.exists(paths[2]):
 
44
  sh = Image.new("RGB", (512, 384), "white")
45
  d = ImageDraw.Draw(sh)
46
  colors = [(220,20,60),(65,105,225),(60,179,113),(255,165,0),(148,0,211)]
@@ -48,9 +51,29 @@ def _ensure_examples():
48
  d.rectangle([20+90*i, 30, 80+90*i, 180], fill=c, outline=(0,0,0), width=3)
49
  for i in range(6):
50
  d.ellipse([40+80*i, 200, 90+80*i, 350], fill=colors[i%len(colors)], outline=(0,0,0), width=3)
51
- sh.save(paths[2])
52
- return [[p] for p in paths]
53
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def _parse_grid(grid_choice: str) -> int:
55
  # Expect strings like "2x2", "4x4", "8x8", "16x16"
56
  try:
@@ -130,11 +153,12 @@ def run_batch(zip_file, grid_choice, use_overlap, overlap_pct, mix_prob, beta_a,
130
  msg = f"Processed {count_out}/{count_in} files."
131
  return out_zip, msg
132
 
 
133
  with gr.Blocks() as demo:
134
  gr.Markdown(f"# {TITLE}")
135
  gr.Markdown(DESC)
136
 
137
- examples = _ensure_examples()
138
 
139
  with gr.Tabs():
140
  with gr.TabItem("Single Image"):
 
12
  """
13
 
14
  EXAMPLES_DIR = "examples"
15
+ CREATE_DEFAULTS_IF_EMPTY = True # set False if you never want auto-generated examples
16
 
17
+ # ---------- Examples handling ----------
18
+ def _make_default_examples():
19
  os.makedirs(EXAMPLES_DIR, exist_ok=True)
20
+
 
 
 
 
21
  # 1) Checkerboard
22
+ cb_path = os.path.join(EXAMPLES_DIR, "checkerboard.png")
23
+ if not os.path.exists(cb_path):
24
  cb = Image.new("RGB", (512, 512), "white")
25
  draw = ImageDraw.Draw(cb)
26
  tile = 64
 
28
  for x in range(0, 512, tile):
29
  if (x//tile + y//tile) % 2 == 0:
30
  draw.rectangle([x, y, x+tile-1, y+tile-1], fill=(30, 30, 30))
31
+ cb.save(cb_path)
32
+
33
  # 2) Gradient
34
+ grad_path = os.path.join(EXAMPLES_DIR, "gradient.png")
35
+ if not os.path.exists(grad_path):
36
  arr = np.zeros((360, 640, 3), dtype=np.uint8)
37
  for x in range(640):
38
  arr[:, x, 0] = int(255 * x / 639)
39
  for y in range(360):
40
  arr[y, :, 1] = int(255 * y / 359)
41
  arr[:, :, 2] = 160
42
+ Image.fromarray(arr).save(grad_path)
43
+
44
  # 3) Shapes
45
+ shapes_path = os.path.join(EXAMPLES_DIR, "shapes.png")
46
+ if not os.path.exists(shapes_path):
47
  sh = Image.new("RGB", (512, 384), "white")
48
  d = ImageDraw.Draw(sh)
49
  colors = [(220,20,60),(65,105,225),(60,179,113),(255,165,0),(148,0,211)]
 
51
  d.rectangle([20+90*i, 30, 80+90*i, 180], fill=c, outline=(0,0,0), width=3)
52
  for i in range(6):
53
  d.ellipse([40+80*i, 200, 90+80*i, 350], fill=colors[i%len(colors)], outline=(0,0,0), width=3)
54
+ sh.save(shapes_path)
55
+
56
+ def _list_example_images():
57
+ """Return [[path], [path], ...] for all images under examples/ (recursive)."""
58
+ exts = {".png", ".jpg", ".jpeg", ".bmp", ".webp"}
59
+ items = []
60
+ if os.path.isdir(EXAMPLES_DIR):
61
+ for root, _, files in os.walk(EXAMPLES_DIR):
62
+ for f in files:
63
+ if os.path.splitext(f)[1].lower() in exts:
64
+ items.append([os.path.join(root, f)])
65
+ # sort by path for stable order
66
+ items.sort(key=lambda x: x[0].lower())
67
+ return items
68
+
69
+ def _get_examples():
70
+ items = _list_example_images()
71
+ if not items and CREATE_DEFAULTS_IF_EMPTY:
72
+ _make_default_examples()
73
+ items = _list_example_images()
74
+ return items
75
+
76
+ # ---------- App logic ----------
77
  def _parse_grid(grid_choice: str) -> int:
78
  # Expect strings like "2x2", "4x4", "8x8", "16x16"
79
  try:
 
153
  msg = f"Processed {count_out}/{count_in} files."
154
  return out_zip, msg
155
 
156
+ # ---------- UI ----------
157
  with gr.Blocks() as demo:
158
  gr.Markdown(f"# {TITLE}")
159
  gr.Markdown(DESC)
160
 
161
+ examples = _get_examples()
162
 
163
  with gr.Tabs():
164
  with gr.TabItem("Single Image"):