2 from .image
import LOCAL, PARENT, ImageOrigin
4 __all__ = [
"supportSlicing"]
8 """Separate the actual slice from origin arguments to __getitem__ or 9 __setitem__, using PARENT for the origin if it is not provided. 13 sliceArgs : `tuple`, `Box2I`, or `Point2I` 14 The first argument passed to an image-like object's 15 ``__getitem__`` or ``__setitem__``. 19 sliceArgs : `tuple`, `Box2I`, or `Point2I` 20 The original sliceArgs passed in, with any ImageOrigin argument 22 origin : `ImageOrigin` 23 Enum value that sets whether or not to consider xy0 in positions. 25 See interpretSliceArgs for more information. 27 Intended primarily for internal use by `supportSlicing()`. 29 defaultOrigin = PARENT
31 if isinstance(sliceArgs[-1], ImageOrigin):
33 if len(sliceArgs) == 2:
34 return sliceArgs[0], sliceArgs[-1]
36 return sliceArgs[:-1], sliceArgs[-1]
39 return sliceArgs, defaultOrigin
41 return sliceArgs, defaultOrigin
45 """Handle negative indices passed to image accessors. 47 When negative indices are used in LOCAL coordinates, we interpret them as 48 relative to the upper bounds of the array, as in regular negative indexing 51 Using negative indices in PARENT coordinates is not allowed unless passed 52 via a `Point2I` or `Box2I`; the potential for confusion between actual 53 negative indices (when ``xy0 < 0``) and offsets relative to the upper 54 bounds of the array is too great. 58 index : `int` or `None` 59 1-d pixel index to interpret, as given by a caller to an image-like 60 object's ``__getitem__`` or ``__setitem__``. 62 Size of the image in the dimension corresponding to ``index``. 63 origin : `ImageOrigin` 64 Enum value that sets whether or not to consider xy0 in positions. 66 Index to return if `index` is None. 71 If ``origin==PARENT``, either the given ``index`` or ``default``. 72 If ``origin==LOCAL``, an equivalent index guaranteed to be nonnegative. 74 Intended primarily for internal use by `supportSlicing()`. 77 assert default
is not None 83 raise IndexError(
"Negative indices are not permitted with the PARENT origin. " 84 "Use LOCAL to use negative to index relative to the end, " 85 "and Point2I or Box2I indexing to access negative pixels " 86 "in PARENT coordinates.")
91 """Create a Point2I from an x, y pair, correctly handling negative indices. 99 origin : `ImageOrigin` 100 Enum value that sets whether or not to consider xy0 in positions. 102 Bounding box of the image being indexed, obtained using 108 A point corresponding to the given indices. 110 Intended primarily for internal use by `supportSlicing()`. 119 """Transform a tuple of slice objects into a Box2I, correctly handling negative indices. 124 Column index range. Step must be None. 126 Row index range. Step must be None. 127 origin : `ImageOrigin` 128 Enum value that sets whether or not to consider xy0 in positions. 130 Bounding box of the image being indexed, obtained using 136 A point corresponding to the given indices. 138 Intended primarily for internal use by `supportSlicing()`. 140 if x.step
is not None or y.step
is not None:
141 raise ValueError(
"Slices with steps are not supported in image indexing.")
150 return Box2I(begin, end - begin)
154 """Transform arguments to __getitem__ or __setitem__ to a standard form. 158 sliceArgs : `tuple`, `Box2I`, or `Point2I` 159 Slice arguments passed directly to `__getitem__` or `__setitem__`. 160 bboxGetter : callable 161 Callable that accepts an ImageOrigin enum value and returns the 162 appropriate image bounding box. Usually the bound getBBox method 163 of an Image, Mask, or MaskedImage object. 167 box : `Box2I` or `None` 168 A box to use to create a subimage, or None if the slice refers to a 170 index: `tuple` or `None` 171 An ``(x, y)`` tuple of integers, or None if the slice refers to a 173 origin : `ImageOrigin` 174 Enum indicating whether to account for xy0. 176 Intended primarily for internal use by `supportSlicing()`. 179 if isinstance(slices, Point2I):
180 return None, slices, origin
181 elif isinstance(slices, Box2I):
182 return slices,
None, origin
183 elif isinstance(slices, slice):
184 if slices.start
is not None or slices.stop
is not None or slices.step
is not None:
185 raise TypeError(
"Single-dimension slices must not have bounds.")
191 if isinstance(x, slice):
192 if isinstance(y, slice):
193 return makeBoxFromSlices(x, y, origin=origin, bbox=bboxGetter(origin)),
None, origin
194 raise TypeError(
"Mixed indices of the form (slice, int) are not supported for images.")
196 if isinstance(y, slice):
197 raise TypeError(
"Mixed indices of the form (int, slice) are not supported for images.")
202 """Support image slicing 205 def Factory(self, *args, **kwargs):
206 """Return an object of this type 208 return cls(*args, **kwargs)
209 cls.Factory = Factory
212 """Return a deep copy of self""" 213 return cls(self,
True)
216 def __getitem__(self, imageSlice):
219 return self.subset(box, origin=origin)
220 return self._get(index, origin=origin)
221 cls.__getitem__ = __getitem__
223 def __setitem__(self, imageSlice, rhs):
226 if self.assign(rhs, box, origin)
is NotImplemented:
227 lhs = self.subset(box, origin=origin)
230 self._set(index, origin=origin, value=rhs)
231 cls.__setitem__ = __setitem__
def makePointFromIndices(x, y, origin, bbox)
def interpretSliceArgs(sliceArgs, bboxGetter)
def splitSliceArgs(sliceArgs)
def makeBoxFromSlices(x, y, origin, bbox)
def handleNegativeIndex(index, size, origin, default)