Improve /scripts/segdefs*.py to allow multiple outlines, revert /scripts/to_mono...
authorNick Downing <nick@ndcode.org>
Thu, 17 Jul 2025 00:48:00 +0000 (10:48 +1000)
committerNick Downing <nick@ndcode.org>
Thu, 17 Jul 2025 00:48:00 +0000 (10:48 +1000)
scripts/segdefs.py
scripts/segdefs_nets.py
scripts/to_mono.py

index 0b9cf7c..da549a0 100755 (executable)
@@ -109,9 +109,22 @@ for colour in sorted(colours):
 
   for _, _, colour1, coords in segments:
     if colour1 == colour:
-      x, y = coords[-1]
-      move_to(x, y)
-      for x, y in coords:
+      # there may be a bug with repeated first coordinate in some versions?
+      assert segments[1] != segments[0]
+      # track first coordinate of outline (repeats to close and start new)
+      coord0 = None
+      for coord in coords:
+        x, y = coord
+        if coord0 is None:
+          coord0 = coord
+          move_to(x, y)
+        else:
+          if coord == coord0:
+            coord = None
+          line_to(x, y)
+      # if first coordinate was not repeated then close outline implicitly
+      if coord0 is not None:
+        x, y = coord0
         line_to(x, y)
       flush_edges()
   PIL.Image.fromarray(image).save(f'{colour:d}.png')
index 482c305..d733498 100755 (executable)
@@ -126,9 +126,22 @@ for net in sorted(nets.keys()):
     for segment in nets[net]:
       _, _, colour, coords = segments[segment]
       if colour in colours:
-        x, y = coords[-1]
-        move_to(x, y)
-        for x, y in coords:
+        # there may be a bug with repeated first coordinate in some versions?
+        assert segments[1] != segments[0]
+        # track first coordinate of outline (repeats to close and start new)
+        coord0 = None
+        for coord in coords:
+          x, y = coord
+          if coord0 is None:
+            coord0 = coord
+            move_to(x, y)
+          else:
+            if coord == coord0:
+              coord = None
+            line_to(x, y)
+        # if first coordinate was not repeated then close outline implicitly
+        if coord0 is not None:
+          x, y = coord0
           line_to(x, y)
         flush_edges()
     images.append(image)
index e4b2b3f..4867abc 100755 (executable)
@@ -25,7 +25,7 @@ image = numpy.array(image)
 #print('dtype', image.dtype)
 assert len(image.shape) == 3 and image.dtype == numpy.uint8
 
-image = image != 0
+image = image >= 0x80
 if image.shape[2] == 2 or image.shape[2] == 4:
   image = image[:, :, :-1] & image[:, :, -1:]
 PIL.Image.fromarray(numpy.any(image, 2)).save(image_out)