def draw_text(self, image: PillowImage, text: List[str], font: PillowImageFont): draw = ImageDraw.Draw(image) for line_index, line in enumerate(text): font_width, _ = font.getsize(line) _, height_offset = font.getoffset(line) x = self._get_x(font_width) y = self._get_y(line_index, self.line_height, height_offset) for c in line: if not is_emoji(c) and c.isprintable(): draw.text((x, y), c, font=font, fill=self.font_color) x += math.ceil(draw.textsize(c, font)[0]) elif self.get_emoji_content: emoji_content = self.get_emoji_content(c) if emoji_content: with tempfile.NamedTemporaryFile(mode='wb') as f: temp_filename = f.name f.write(emoji_content) emoji_img = Image.open(temp_filename) resized_emoji_img = emoji_img.resize( (font.size, font.size)) image.paste(resized_emoji_img, (x, y), resized_emoji_img) x += math.ceil(resized_emoji_img.width) return image
def getoffset(font: FreeTypeFont, text_str: str) -> Tuple[int, int]: """get the offset of text""" if USE_PIL: offset = font.getoffset(text_str) else: # FOR NOW...because I don't think this is truly necessary offset = (0, 0) return offset
def draw_text(self, image: PillowImage, text: List[str], font: PillowImageFont): draw = ImageDraw.Draw(image) for line_index, line in enumerate(text): font_width, _ = font.getsize(line) _, height_offset = font.getoffset(line) x = self._get_x(font_width) y = self._get_y(line_index, self.line_height, height_offset) draw.text((x, y), line, font=font, fill=self.font_color) return image