1 from __future__
import absolute_import, division, print_function
4 from .image
import LOCAL, PARENT, ImageOrigin
6 __all__ = [
"supportSlicing"]
9 def _getBBoxFromSliceTuple(img, imageSlice):
10 """Given a slice specification return the proper Box2I and origin 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) 35 You may also add an extra argument, afwImage.PARENT or afwImage.LOCAL. The default is LOCAL 36 as before, but if you specify PARENT the bounding box is interpreted in PARENT coordinates 37 (this includes slices; e.g. 38 im[-3:, -2:, afwImage.PARENT] 39 still means the last few rows and columns, and 40 im[1001:1004, 2006:2010, afwImage.PARENT] 41 with xy0 = (1000, 2000) refers to the same pixels as 42 im[1:4, 6:10, afwImage.LOCAL] 48 _origin = imageSlice[-1]
52 if isinstance(_origin, ImageOrigin):
54 imageSlice = imageSlice[0]
if len(imageSlice) <= 2
else imageSlice[:-1]
57 return imageSlice, origin
59 if isinstance(imageSlice, slice)
and imageSlice.start
is None and imageSlice.stop
is None:
60 imageSlice = (Ellipsis, Ellipsis,)
62 if not (isinstance(imageSlice, tuple)
and len(imageSlice) == 2
and 63 sum([isinstance(_, (slice, type(Ellipsis), int))
for _
in imageSlice]) == 2):
65 "Images may only be indexed as a 2-D slice not %s" % imageSlice)
71 imageSlice, _imageSlice = [], imageSlice
72 for s, wh, z0
in zip(_imageSlice, img.getDimensions(), img.getXY0()):
76 if isinstance(s, slice):
78 start = s.start
if s.start
is None or s.start < 0
else s.start - z0
79 stop = s.stop
if s.stop
is None or s.stop < 0
else s.stop - z0
80 s = slice(start, stop, s.step)
81 elif isinstance(s, int):
84 s = slice(s - z0, s - z0 + 1)
90 x, y = [_.indices(wh)
for _, wh
in zip(imageSlice, img.getDimensions())]
95 """Support image slicing 98 def _checkOrigin(origin):
99 if origin
not in (LOCAL, PARENT):
100 raise RuntimeError(
"keyword origin must be afwImage.ORIGIN or afwImage.PARENT, not %s" % origin)
102 def Factory(self, *args, **kwargs):
103 """Return an object of this type 105 return cls(*args, **kwargs)
106 cls.Factory = Factory
109 """Return a deep copy of self""" 110 return cls(self,
True)
113 def __getitem__(self, imageSlice):
114 bbox, origin = _getBBoxFromSliceTuple(self, imageSlice)
117 return self.Factory(self, bbox, origin)
118 cls.__getitem__ = __getitem__
120 def __setitem__(self, imageSlice, rhs):
121 bbox, origin = _getBBoxFromSliceTuple(self, imageSlice)
124 if self.assign(rhs, bbox, origin)
is NotImplemented:
125 lhs = self.Factory(self, bbox, origin=origin)
127 cls.__setitem__ = __setitem__
130 """Convert a 1x1 image to a floating scalar""" 133 "Only single-pixel images may be converted to python scalars")
136 return float(self.get(0, 0))
137 except AttributeError:
139 "Unable to extract a single pixel from a {}".format(type(self).__name__))
142 "Unable to convert a {} pixel to a scalar".format(type(self).__name__))
143 cls.__float__ = __float__
146 """Convert a 1x1 image to a integral scalar""" 147 return int(float(self))
148 cls.__int__ = __int__
An integer coordinate rectangle.