File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 } "
You can’t perform that action at this time.
0 commit comments