1 from __future__
import absolute_import, division, print_function
4 from .image
import LOCAL
6 __all__ = [
"supportSlicing"]
9 def _getBBoxFromSliceTuple(img, imageSlice):
10 """Given a slice specification return the proper Box2I
11 This is the worker routine behind __getitem__ and __setitem__
13 The imageSlice may be:
17 Only the first one or two parts of the slice are recognised (no stride), a single int is
18 interpreted as n:n+1, and negative indices are interpreted relative to the end of the array,
19 so supported slices include:
24 : (equivalent to ... (python's Ellipsis) which is also supported)
38 if isinstance(imageSlice, slice)
and imageSlice.start
is None and imageSlice.stop
is None:
39 imageSlice = (Ellipsis, Ellipsis,)
41 if not (isinstance(imageSlice, tuple)
and len(imageSlice) == 2
and
42 sum([isinstance(_, (slice, type(Ellipsis), int))
for _
in imageSlice]) == 2):
44 "Images may only be indexed as a 2-D slice not %s", imageSlice)
46 imageSlice, _imageSlice = [], imageSlice
47 for s, wh
in zip(_imageSlice, img.getDimensions()):
48 if isinstance(s, slice):
50 elif isinstance(s, int):
59 x, y = [_.indices(wh)
for _, wh
in zip(imageSlice, img.getDimensions())]
64 """Support image slicing
67 def Factory(self, *args):
68 """Return an object of this type
74 """Return a deep copy of self"""
75 return cls(self,
True)
79 return self.Factory(self, _getBBoxFromSliceTuple(self, imageSlice), LOCAL)
80 cls.__getitem__ = __getitem__
83 bbox = _getBBoxFromSliceTuple(self, imageSlice)
85 if self.assign(rhs, bbox, LOCAL)
is NotImplemented:
86 lhs = self.Factory(self, bbox, LOCAL)
88 cls.__setitem__ = __setitem__
91 """Convert a 1x1 image to a floating scalar"""
94 "Only single-pixel images may be converted to python scalars")
97 return float(self.get(0, 0))
98 except AttributeError:
100 "Unable to extract a single pixel from a {}".format(type(self).__name__))
103 "Unable to convert a {} pixel to a scalar".format(type(self).__name__))
104 cls.__float__ = __float__
107 """Convert a 1x1 image to a integral scalar"""
108 return int(float(self))
109 cls.__int__ = __int__
An integer coordinate rectangle.