From 9dca738cdf6807c888a263b157024e9805cd04b4 Mon Sep 17 00:00:00 2001 From: aclegg3 Date: Wed, 1 Mar 2023 12:37:39 -0800 Subject: [PATCH 1/2] fix color overflow bug --- src_python/habitat_sim/utils/viz_utils.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src_python/habitat_sim/utils/viz_utils.py b/src_python/habitat_sim/utils/viz_utils.py index fedea6b7ec..d8e54cb15b 100644 --- a/src_python/habitat_sim/utils/viz_utils.py +++ b/src_python/habitat_sim/utils/viz_utils.py @@ -12,6 +12,7 @@ if "google.colab" in sys.modules: os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg" +import random from typing import Any, Dict, List, Optional, Tuple import imageio @@ -300,11 +301,22 @@ def get_island_colored_map(island_top_down_map_data: np.ndarray): white = int("0xffffff", base=16) island_map = Image.new("RGB", island_top_down_map_data.shape, color=white) pixels = island_map.load() + extra_colors: List[int] = [] for x in range(island_top_down_map_data.shape[0]): for y in range(island_top_down_map_data.shape[1]): if island_top_down_map_data[x, y] >= 0: - pixels[x, y] = int( - d3_40_colors_hex[island_top_down_map_data[x, y]], base=16 - ) - + color_index = island_top_down_map_data[x, y] + if color_index < len(d3_40_colors_hex): + # fixed colors from a selected list + # NOTE: PIL Image origin is top left, so invert y + pixels[x, -y] = int(d3_40_colors_hex[color_index], base=16) + else: + random_color_index = color_index - len(d3_40_colors_hex) + # pick random colors once fixed colors are overflowed + while random_color_index >= len(extra_colors): + r = lambda: random.randint(0, 255) + new_color = int(("0x%02X%02X%02X" % (r(), r(), r())), base=16) + if new_color not in extra_colors: + extra_colors.append(new_color) + pixels[x, -y] = extra_colors[random_color_index] return island_map From f0629997e104767ad52a83e331e9114b930f17ee Mon Sep 17 00:00:00 2001 From: aclegg3 Date: Wed, 1 Mar 2023 13:03:53 -0800 Subject: [PATCH 2/2] move the lambda --- src_python/habitat_sim/utils/viz_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src_python/habitat_sim/utils/viz_utils.py b/src_python/habitat_sim/utils/viz_utils.py index d8e54cb15b..926441e155 100644 --- a/src_python/habitat_sim/utils/viz_utils.py +++ b/src_python/habitat_sim/utils/viz_utils.py @@ -302,6 +302,7 @@ def get_island_colored_map(island_top_down_map_data: np.ndarray): island_map = Image.new("RGB", island_top_down_map_data.shape, color=white) pixels = island_map.load() extra_colors: List[int] = [] + r = lambda: random.randint(0, 255) for x in range(island_top_down_map_data.shape[0]): for y in range(island_top_down_map_data.shape[1]): if island_top_down_map_data[x, y] >= 0: @@ -314,7 +315,6 @@ def get_island_colored_map(island_top_down_map_data: np.ndarray): random_color_index = color_index - len(d3_40_colors_hex) # pick random colors once fixed colors are overflowed while random_color_index >= len(extra_colors): - r = lambda: random.randint(0, 255) new_color = int(("0x%02X%02X%02X" % (r(), r(), r())), base=16) if new_color not in extra_colors: extra_colors.append(new_color)