82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
from PIL import Image, ImageDraw
|
|
|
|
from cv_analysis.utils.image_operations import superimpose
|
|
from synthesis.segment.content_rectangle import ContentRectangle
|
|
|
|
|
|
class Cell(ContentRectangle):
|
|
def __init__(self, x1, y1, x2, y2, color=None):
|
|
super().__init__(x1, y1, x2, y2)
|
|
|
|
self.background_color = color or (255, 255, 255, 0)
|
|
|
|
# to debug use random border color: tuple([random.randint(100, 200) for _ in range(3)] + [255])
|
|
self.cell_border_color = (0, 0, 0, 255)
|
|
|
|
self.border_width = 1
|
|
self.inset = 1
|
|
|
|
self.content = Image.new("RGBA", (self.width, self.height))
|
|
self.fill()
|
|
|
|
def draw_top_border(self, width=None):
|
|
self.draw_line((0, 0, self.width - self.inset, 0), width=width)
|
|
return self
|
|
|
|
def draw_bottom_border(self, width=None):
|
|
self.draw_line((0, self.height - self.inset, self.width - self.inset, self.height - self.inset), width=width)
|
|
return self
|
|
|
|
def draw_left_border(self, width=None):
|
|
self.draw_line((0, 0, 0, self.height), width=width)
|
|
return self
|
|
|
|
def draw_right_border(self, width=None):
|
|
self.draw_line((self.width - self.inset, 0, self.width - self.inset, self.height), width=width)
|
|
return self
|
|
|
|
def draw_line(self, points, width=None):
|
|
width = width or self.border_width
|
|
draw = ImageDraw.Draw(self.content)
|
|
draw.line(points, width=width, fill=self.cell_border_color)
|
|
return self
|
|
|
|
def draw(self, width=None):
|
|
self.draw_top_border(width=width)
|
|
self.draw_bottom_border(width=width)
|
|
self.draw_left_border(width=width)
|
|
self.draw_right_border(width=width)
|
|
return self
|
|
|
|
def draw_top_left_corner(self, width=None):
|
|
self.draw_line((0, 0, 0, 0), width=width)
|
|
self.draw_line((0, 0, 0, 0), width=width)
|
|
return self
|
|
|
|
def draw_top_right_corner(self, width=None):
|
|
self.draw_line((self.width - self.inset, 0, self.width - self.inset, 0), width=width)
|
|
self.draw_line((self.width - self.inset, 0, self.width - self.inset, 0), width=width)
|
|
return self
|
|
|
|
def draw_bottom_left_corner(self, width=None):
|
|
self.draw_line((0, self.height - self.inset, 0, self.height - self.inset), width=width)
|
|
self.draw_line((0, self.height - self.inset, 0, self.height - self.inset), width=width)
|
|
return self
|
|
|
|
def draw_bottom_right_corner(self, width=None):
|
|
self.draw_line(
|
|
(self.width - self.inset, self.height - self.inset, self.width - self.inset, self.height - self.inset),
|
|
width=width,
|
|
)
|
|
self.draw_line(
|
|
(self.width - self.inset, self.height - self.inset, self.width - self.inset, self.height - self.inset),
|
|
width=width,
|
|
)
|
|
return self
|
|
|
|
def fill(self, color=None):
|
|
color = color or self.background_color
|
|
image = Image.new("RGBA", (self.width, self.height), color=color)
|
|
self.content = superimpose(image, self.content)
|
|
return self
|