blanchon commited on
Commit
5f528b8
·
1 Parent(s): f41b71e
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -27,12 +27,24 @@ def encode_image_as_base64(image: Image.Image) -> str:
27
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
28
 
29
 
30
- def make_example(image_path: Path) -> EditorValue:
31
  background_image = Image.open(image_path)
32
  background_image = background_image.convert("RGB")
33
  background = np.array(background_image)
34
 
35
- mask = np.zeros_like(background)
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  layer = np.zeros((background.shape[0], background.shape[1], 4), dtype=np.uint8)
38
  layer[:, :, 3] = mask
@@ -198,7 +210,7 @@ with gr.Blocks(css=css) as demo:
198
  )
199
  image_and_mask_examples = gr.Examples(
200
  examples=[
201
- make_example(path)
202
  for path in Path("./examples/scenes").glob("*.png")
203
  ],
204
  label="Room examples",
 
27
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
28
 
29
 
30
+ def make_example(image_path: Path, mask_path: Path | None) -> EditorValue:
31
  background_image = Image.open(image_path)
32
  background_image = background_image.convert("RGB")
33
  background = np.array(background_image)
34
 
35
+ if mask_path:
36
+ mask_image = Image.open(mask_path)
37
+ mask_image = mask_image.convert("RGB")
38
+ mask = np.array(mask_image)
39
+ mask = mask[:, :, 0]
40
+ mask = np.where(mask == 255, 0, 255) # noqa: PLR2004
41
+ else:
42
+ mask = np.zeros_like(background)
43
+ mask = mask[:, :, 0]
44
+
45
+ if background.shape[0] != mask.shape[0] or background.shape[1] != mask.shape[1]:
46
+ msg = "Background and mask must have the same shape"
47
+ raise ValueError(msg)
48
 
49
  layer = np.zeros((background.shape[0], background.shape[1], 4), dtype=np.uint8)
50
  layer[:, :, 3] = mask
 
210
  )
211
  image_and_mask_examples = gr.Examples(
212
  examples=[
213
+ make_example(path, None)
214
  for path in Path("./examples/scenes").glob("*.png")
215
  ],
216
  label="Room examples",