diff --git a/pixelocr/image.py b/pixelocr/image.py index 2cbb984..74c601f 100644 --- a/pixelocr/image.py +++ b/pixelocr/image.py @@ -31,16 +31,13 @@ def _is_nonblank(bitmap): class Image(object): """Basic image class.""" - def __init__(self, data): - self.parent = self + def __init__(self, data, y1=0, x1=0): self.data = data - self.y1 = 0 - self.y2 = self.width - self.x1 = 0 - self.x2 = self.height + self.y1 = y1 + self.x1 = x1 def __getitem__(self, key): - """Return a SubImage for the specified region.""" + """Return an Image for the specified region.""" def indices(sliceobj, length): """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) y1 = self.y1 + ystart - y2 = self.y1 + yend x1 = self.x1 + xstart - x2 = self.x1 + xend - return SubImage(self.parent, y1, y2, x1, x2) + return Image(self.data[key], y1, x1) def _repr_png_(self): buf = BytesIO() @@ -80,14 +75,18 @@ class Image(object): def fromfile(cls, filename): return cls(imread(filename)) + @property + def y2(self): + return self.y1 + self.height + + @property + def x2(self): + return self.x1 + self.width + @property def shape(self): return self.data.shape - @cached_property - def T(self): - return type(self)(self.data.swapaxes(0, 1)) - @property def height(self): return self.data.shape[0] @@ -96,6 +95,10 @@ class Image(object): def width(self): 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 def bitmap(self): """Return a two-color version of the image. @@ -181,24 +184,3 @@ class Image(object): yield prev_line 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)