Added color options and more differenciated masking options for special cases
This commit is contained in:
parent
06b6d697f2
commit
91567bef60
2 changed files with 20 additions and 7 deletions
|
@ -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
|
||||
pass
|
||||
|
||||
def __get_mask__ (self, image):
|
||||
mask = image.convert('L').point(lambda p : 0 if p is 0 else 1 )
|
||||
return ImageOps.invert(mask)
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue