Skip to content

Commit 8eb42c0

Browse files
committed
check impage format and shape against buffer
1 parent bf84c57 commit 8eb42c0

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

examples/guis/imgui_file_dialog.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,25 @@ def _load_image(self, filepath):
7474
"""Load an image file and update the graphic."""
7575
try:
7676
img_data = iio.imread(filepath)
77-
self._image_graphic.data = img_data
77+
78+
# validate image format: must be 2D (grayscale) or 3D with 1-4 channels
79+
if not (img_data.ndim == 2 or (img_data.ndim == 3 and img_data.shape[2] <= 4)):
80+
self._status_msg = (
81+
f"Unsupported format: {img_data.shape}. "
82+
f"Expected (H, W) or (H, W, C) with C <= 4"
83+
)
84+
return
85+
86+
# check if the new image shape matches the current buffer
87+
if img_data.shape == self._image_graphic.data.value.shape:
88+
self._image_graphic.data = img_data
89+
else:
90+
# shape mismatch: remove old graphic and create a new one
91+
subplot = self._image_graphic._plot_area
92+
subplot.remove_graphic(self._image_graphic)
93+
self._image_graphic = subplot.add_image(img_data)
94+
subplot.auto_scale()
95+
7896
self._status_msg = f"Loaded: {filepath}"
7997
except Exception as e:
8098
self._status_msg = f"Error: {e}"

0 commit comments

Comments
 (0)