MichaelRKessler commited on
Commit
1ccc4b3
·
1 Parent(s): 987553b

Change black as material and white as empty space

Browse files
Files changed (3) hide show
  1. README.md +1 -0
  2. stl_slicer.py +3 -3
  3. tests/test_stl_slicer.py +1 -2
README.md CHANGED
@@ -27,6 +27,7 @@ Then open the local Gradio URL in your browser, upload an STL file, and generate
27
  - Shows an interactive 3D viewer for rotating the model
28
  - Lets you choose layer height and XY pixel size
29
  - Produces one `.tif` image per slice
 
30
  - Lets you step through the slice stack in the browser
31
  - Exports a ZIP containing the generated TIFF images
32
 
 
27
  - Shows an interactive 3D viewer for rotating the model
28
  - Lets you choose layer height and XY pixel size
29
  - Produces one `.tif` image per slice
30
+ - Encodes material as black (`0`) and empty space as white (`255`) in each TIFF slice
31
  - Lets you step through the slice stack in the browser
32
  - Exports a ZIP containing the generated TIFF images
33
 
stl_slicer.py CHANGED
@@ -129,7 +129,7 @@ def _render_slice(
129
  image_size: tuple[int, int],
130
  pixel_size: float,
131
  ) -> Image.Image:
132
- image = Image.new("L", image_size, 0)
133
 
134
  if section is None:
135
  return image
@@ -142,12 +142,12 @@ def _render_slice(
142
  for exterior, holes in polygons:
143
  draw.polygon(
144
  _to_pixel_ring(exterior, x_min, y_min, pixel_size, image.height),
145
- fill=255,
146
  )
147
  for hole in holes:
148
  draw.polygon(
149
  _to_pixel_ring(hole, x_min, y_min, pixel_size, image.height),
150
- fill=0,
151
  )
152
 
153
  return image
 
129
  image_size: tuple[int, int],
130
  pixel_size: float,
131
  ) -> Image.Image:
132
+ image = Image.new("L", image_size, 255)
133
 
134
  if section is None:
135
  return image
 
142
  for exterior, holes in polygons:
143
  draw.polygon(
144
  _to_pixel_ring(exterior, x_min, y_min, pixel_size, image.height),
145
+ fill=0,
146
  )
147
  for hole in holes:
148
  draw.polygon(
149
  _to_pixel_ring(hole, x_min, y_min, pixel_size, image.height),
150
+ fill=255,
151
  )
152
 
153
  return image
tests/test_stl_slicer.py CHANGED
@@ -34,8 +34,7 @@ def test_slice_stl_to_tiffs_creates_non_empty_tiffs(tmp_path) -> None:
34
  with Image.open(stack.tiff_paths[0]) as first_image:
35
  pixels = np.array(first_image)
36
 
37
- assert pixels.max() == 255
38
- assert pixels.sum() > 0
39
 
40
 
41
  def test_compose_even_odd_polygons_preserves_holes() -> None:
 
34
  with Image.open(stack.tiff_paths[0]) as first_image:
35
  pixels = np.array(first_image)
36
 
37
+ assert np.any(pixels == 0)
 
38
 
39
 
40
  def test_compose_even_odd_polygons_preserves_holes() -> None: