From 91567bef6055522f9b6ba3f441e5d6ae252666e2 Mon Sep 17 00:00:00 2001 From: Max G Date: Thu, 7 Mar 2019 21:27:13 +0100 Subject: [PATCH] Added color options and more differenciated masking options for special cases --- Calendar/DesignEntity.py | 17 ++++++++++++----- Calendar/TextDesign.py | 10 ++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Calendar/DesignEntity.py b/Calendar/DesignEntity.py index 1e6458b..2a12e20 100644 --- a/Calendar/DesignEntity.py +++ b/Calendar/DesignEntity.py @@ -3,10 +3,11 @@ from PIL import Image, ImageOps, ImageDraw class DesignEntity (object): """General entity that can be drawn on to a panel design or other design entities.""" - def __init__ (self, size, mask=False): + def __init__ (self, size, mask=False, invert_mask=False): self.size = size self.pos = (0, 0) self.mask = mask + self.invert_mask = invert_mask self.__init_image__() self.__finished_image__ = False @@ -19,17 +20,23 @@ class DesignEntity (object): self.__finished_image__ = True return self.__image__ - def draw (self, subimage, pos, mask=False): + def draw (self, subimage, pos, mask=False, invert_mask=False): img_mask = None if mask: - img_mask = ImageOps.invert(subimage.convert('L')) + img_mask = self.__get_mask__(subimage) + if invert_mask: + img_mask = ImageOps.invert(img_mask) self.__image__.paste(subimage, pos, mask=img_mask) def draw_design (self, entity): - self.draw(entity.get_image(), entity.pos, entity.mask) + self.draw(entity.get_image(), entity.pos, entity.mask, entity.invert_mask) def draw_image (self, path, pos): self.draw(Image.open(path), pos) def __finish_image__ (self): - pass \ No newline at end of file + pass + + def __get_mask__ (self, image): + mask = image.convert('L').point(lambda p : 0 if p is 0 else 1 ) + return ImageOps.invert(mask) \ No newline at end of file diff --git a/Calendar/TextDesign.py b/Calendar/TextDesign.py index 8640e1d..e2b4453 100644 --- a/Calendar/TextDesign.py +++ b/Calendar/TextDesign.py @@ -8,7 +8,7 @@ paddingcorrection = -5 class TextDesign (DesignEntity): """Object that manages all information relevant to text and prints it to an image""" - def __init__ (self, size, font = None, fontsize = 12, text = "", horizontalalignment = "left", verticalalignment = "top", mask=True, truncate=True, truncate_suffix = '...', wrap=False): + def __init__ (self, size, color="black", background_color="white", font = None, fontsize = 12, text = "", horizontalalignment = "left", verticalalignment = "top", mask=True, truncate=False, truncate_suffix = '...', wrap=False): super(TextDesign, self).__init__(size, mask = mask) if font is None: font = defaultfont @@ -20,15 +20,21 @@ class TextDesign (DesignEntity): self.truncate = truncate self.truncate_suffix = truncate_suffix self.wrap = wrap + self.color = color + self.background_color = background_color def __finish_image__ (self): + if self.color is "white": + self.invert_mask = True + self.__init_image__(self.background_color) + self.__font__ = self.__get_font__() if self.wrap is False and self.truncate: self.__truncate_text__() if self.wrap: self.__wrap_text__() pos = self.__pos_from_alignment__() - ImageDraw.Draw(self.__image__).text(pos, self.text, fill=0, font=self.__font__) + ImageDraw.Draw(self.__image__).text(pos, self.text, fill=self.color, font=self.__font__) def __truncate_text__ (self): if self.__font__.getsize(self.text)[0] < self.size[0]: #does not need truncating