From 8f34ff5cb95fabda7963cb7dacad4b6b2def5986 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Thu, 26 Nov 2026 09:42:45 +1000 Subject: [PATCH] fix: Image[...] with a list of planes raises, and a plane index works on a greyscale image img[[0, 2]] returned a 1x1 image, because a list was read as a pair of keys. It now raises ValueError and points to plane(). An int key goes through plane(), so a greyscale image accepts img[0] and an out-of-range index raises ValueError, not NumPy's IndexError. NumPy integers are accepted, a negative index still counts from the last plane, and True and False are refused. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageCore.py | 16 +++-- tests/test_image_core.py | 85 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/machinevisiontoolbox/ImageCore.py b/src/machinevisiontoolbox/ImageCore.py index cce1a0bd..59d5c1e6 100644 --- a/src/machinevisiontoolbox/ImageCore.py +++ b/src/machinevisiontoolbox/ImageCore.py @@ -2821,13 +2821,21 @@ def __getitem__( ): # integer keys for row and column, the result is a scalar or vector over planes return self._A[keys[1], keys[0], keys[2]] - elif isinstance(keys, int): - # single integer index, it's a color plane index - return self.__class__(self._A[..., keys]) + elif isinstance(keys, (int, np.integer)) and not isinstance(keys, bool): + # single integer index, it's a color plane index; as for NumPy, a + # negative index counts from the last plane + if -self.nplanes <= keys < 0: + keys += self.nplanes + return self.plane(int(keys)) elif isinstance(keys, str): # color plane by name return self.plane(keys) - elif isinstance(keys, (list, tuple)): + elif isinstance(keys, list): + raise ValueError( + f"invalid index {keys!r}: a list is not a valid key, use " + "plane() to select a list of planes" + ) + elif isinstance(keys, tuple): # by slices if self.iscolor: if len(keys) == 2: diff --git a/tests/test_image_core.py b/tests/test_image_core.py index 944b1c5e..e9ea53da 100644 --- a/tests/test_image_core.py +++ b/tests/test_image_core.py @@ -188,6 +188,91 @@ def test_getitem_invalid_key_type(self): with self.assertRaises(ValueError): im[1.5] + def test_getitem_plane_of_a_color_image(self): + a = np.arange(60).reshape((5, 4, 3)) + im = Image(a, colororder="RGB") + for i in range(3): + nt.assert_array_equal(im[i].array, a[:, :, i]) + nt.assert_array_equal(im[np.int64(1)].array, a[:, :, 1]) # NumPy integer + nt.assert_array_equal(im[-1].array, a[:, :, 2]) # counts from the last plane + nt.assert_array_equal(im[-3].array, a[:, :, 0]) + + def test_getitem_plane_out_of_range(self): + im = Image(np.zeros((5, 4, 3)), colororder="RGB") + for key in (3, 5, -4): + with self.assertRaises(ValueError): + im[key] + + def test_getitem_plane_of_a_greyscale_image(self): + a = np.arange(20).reshape((5, 4)) + im = Image(a) + nt.assert_array_equal(im[0].array, a) + nt.assert_array_equal(im[-1].array, a) + with self.assertRaises(ValueError): + im[1] + + def test_getitem_bool_is_not_a_plane(self): + im = Image(np.zeros((5, 4, 3)), colororder="RGB") + with self.assertRaises(ValueError): + im[True] + + def test_getitem_list_is_refused(self): + # used to return a 1x1 image, as if it were the pair of keys (0, 2) + im = Image(np.zeros((5, 4, 3)), colororder="RGB") + with self.assertRaisesRegex(ValueError, "plane"): + im[[0, 2]] + + def test_getitem_lone_slice_is_refused(self): + im = Image(np.zeros((5, 4, 3)), colororder="RGB") + for key in (slice(1, 3), slice(None, None, -1)): + with self.assertRaises(ValueError): + im[key] + + def test_getitem_plane_names(self): + a = np.arange(60).reshape((5, 4, 3)) + im = Image(a, colororder="RGB") + bgr = im["BGR"] + self.assertEqual(bgr.colororder_str, "B:G:R") + nt.assert_array_equal(bgr.array, a[:, :, ::-1]) + br = im["B:R"] + self.assertEqual(br.colororder_str, "B:R") + nt.assert_array_equal(br.array, a[:, :, [2, 0]]) + flipped = im[:, :, ::-1] + self.assertEqual(flipped.colororder_str, "B:G:R") + nt.assert_array_equal(flipped.array, a[:, :, ::-1]) + + def test_getitem_views_and_copies(self): + # what the Image.__getitem__ docstring says about sharing pixels + a = np.arange(60, dtype=np.uint8).reshape((5, 4, 3)) + im = Image(a, colororder="RGB") + self.assertTrue(np.shares_memory(im[0].array, a)) # int: a view + self.assertTrue(np.shares_memory(im["G"].array, a)) # one name: a view + self.assertTrue(np.shares_memory(im[1:3, 1:3].array, a)) # slices: a view + self.assertFalse(np.shares_memory(im["BGR"].array, a)) # several names: a copy + self.assertFalse(np.shares_memory(im.plane([0, 2]).array, a)) + + def test_getitem_results_are_not_contiguous(self): + # drawing into these raises, and Image(..., copy=True) is the way out + im = Image(np.zeros((8, 10, 3), dtype=np.uint8), colororder="RGB") + for sub in (im[0], im["BGR"], im[2:6, 2:6], im[:, :, ::-1], im[:, ::-1]): + self.assertFalse(sub.array.flags.c_contiguous) + color = [1] * sub.nplanes + with self.assertRaises(ValueError): + sub.draw_circle((1, 1), 1, color=color) + fixed = Image(sub.array, copy=True) + self.assertTrue(fixed.array.flags.c_contiguous) + fixed.draw_circle((1, 1), 1, color=color) + + def test_planes_are_views(self): + a = np.arange(60, dtype=np.uint8).reshape((5, 4, 3)) + im = Image(a, colororder="RGB") + planes = list(im.planes()) + self.assertEqual(len(planes), 3) + for i, p in enumerate(planes): + nt.assert_array_equal(p.array, a[:, :, i]) + self.assertTrue(np.shares_memory(p.array, a)) + self.assertFalse(p.array.flags.c_contiguous) + def test_colordict(self): cdict = Image.colororder2dict("RGBA") self.assertIsInstance(cdict, dict)