Get rid of image.SubImage.
Every Image now stores its own data itself.
This commit is contained in:
parent
37c38cb9b2
commit
3d2b2ba30a
1 changed files with 17 additions and 35 deletions
|
|
@ -31,16 +31,13 @@ def _is_nonblank(bitmap):
|
||||||
class Image(object):
|
class Image(object):
|
||||||
"""Basic image class."""
|
"""Basic image class."""
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data, y1=0, x1=0):
|
||||||
self.parent = self
|
|
||||||
self.data = data
|
self.data = data
|
||||||
self.y1 = 0
|
self.y1 = y1
|
||||||
self.y2 = self.width
|
self.x1 = x1
|
||||||
self.x1 = 0
|
|
||||||
self.x2 = self.height
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
"""Return a SubImage for the specified region."""
|
"""Return an Image for the specified region."""
|
||||||
|
|
||||||
def indices(sliceobj, length):
|
def indices(sliceobj, length):
|
||||||
"""Decode a slice object and return a pair of end:start indices."""
|
"""Decode a slice object and return a pair of end:start indices."""
|
||||||
|
|
@ -66,10 +63,8 @@ class Image(object):
|
||||||
xstart, xend = indices(xslice, self.width)
|
xstart, xend = indices(xslice, self.width)
|
||||||
|
|
||||||
y1 = self.y1 + ystart
|
y1 = self.y1 + ystart
|
||||||
y2 = self.y1 + yend
|
|
||||||
x1 = self.x1 + xstart
|
x1 = self.x1 + xstart
|
||||||
x2 = self.x1 + xend
|
return Image(self.data[key], y1, x1)
|
||||||
return SubImage(self.parent, y1, y2, x1, x2)
|
|
||||||
|
|
||||||
def _repr_png_(self):
|
def _repr_png_(self):
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
|
|
@ -80,14 +75,18 @@ class Image(object):
|
||||||
def fromfile(cls, filename):
|
def fromfile(cls, filename):
|
||||||
return cls(imread(filename))
|
return cls(imread(filename))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def y2(self):
|
||||||
|
return self.y1 + self.height
|
||||||
|
|
||||||
|
@property
|
||||||
|
def x2(self):
|
||||||
|
return self.x1 + self.width
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def shape(self):
|
def shape(self):
|
||||||
return self.data.shape
|
return self.data.shape
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def T(self):
|
|
||||||
return type(self)(self.data.swapaxes(0, 1))
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def height(self):
|
def height(self):
|
||||||
return self.data.shape[0]
|
return self.data.shape[0]
|
||||||
|
|
@ -96,6 +95,10 @@ class Image(object):
|
||||||
def width(self):
|
def width(self):
|
||||||
return self.data.shape[1]
|
return self.data.shape[1]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def T(self):
|
||||||
|
return type(self)(self.data.swapaxes(0, 1), y1=self.x1, x1=self.y1)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def bitmap(self):
|
def bitmap(self):
|
||||||
"""Return a two-color version of the image.
|
"""Return a two-color version of the image.
|
||||||
|
|
@ -181,24 +184,3 @@ class Image(object):
|
||||||
yield prev_line
|
yield prev_line
|
||||||
|
|
||||||
return merge_lines(iter_lines())
|
return merge_lines(iter_lines())
|
||||||
|
|
||||||
|
|
||||||
class SubImage(Image):
|
|
||||||
def __init__(self, parent, y1, y2, x1, x2):
|
|
||||||
self.parent = parent
|
|
||||||
self.y1 = y1
|
|
||||||
self.y2 = y2
|
|
||||||
self.x1 = x1
|
|
||||||
self.x2 = x2
|
|
||||||
|
|
||||||
@property
|
|
||||||
def data(self):
|
|
||||||
return self.parent.data[self.y1:self.y2, self.x1:self.x2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def bitmap(self):
|
|
||||||
return self.parent.bitmap[self.y1:self.y2, self.x1:self.x2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def T(self):
|
|
||||||
return type(self)(self.parent.T, self.x1, self.x2, self.y1, self.y2)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue