From 648f6aebe9451f71a9c0deac77c2690e4ee71063 Mon Sep 17 00:00:00 2001 From: Nick Zatkovich Date: Mon, 31 Jul 2017 00:13:20 -0700 Subject: [PATCH] textgen: alpha_composite instead of paste pasting with kerning cut off some of the letters --- graphics/text/smtextgen.py | 12 ++++++++++-- graphics/text/textgen.py | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/graphics/text/smtextgen.py b/graphics/text/smtextgen.py index 5dc4128f..47f24f1d 100755 --- a/graphics/text/smtextgen.py +++ b/graphics/text/smtextgen.py @@ -72,9 +72,16 @@ class Font(object): filename = self.char_filename(c) char_image = Image.open(filename) - image.paste(char_image, (x1, y1)) + char_image.load() + self.paste_image(image, char_image, x1, y1) x1 += self.char_width(c) + def paste_image(self, image, src, x, y): + int_image = Image.new("RGBA", image.size, (0, 0, 0, 0)) + int_image.paste(src, (x, y)) + image = Image.alpha_composite(image, int_image) + + def parse_command_line(args): if len(args) < 4 or (len(args) % 2) != 0: return None @@ -108,6 +115,7 @@ def parse_command_line(args): return result + if __name__ == '__main__': args = parse_command_line(sys.argv[1:]) @@ -138,7 +146,7 @@ if __name__ == '__main__': if string.startswith('file:'): src_image = Image.open(string[5:]) src_image.load() - image.paste(src_image, (xy[0], xy[1])) + smallfont.paste_image(image, src_image, xy[0], xy[1]) else: smallfont.draw_for_text(image, string, xy[0], xy[1]) diff --git a/graphics/text/textgen.py b/graphics/text/textgen.py index 327534ac..c93ef695 100755 --- a/graphics/text/textgen.py +++ b/graphics/text/textgen.py @@ -133,7 +133,9 @@ class Font(object): filename = self.char_filename(c) char_image = Image.open(filename) char_image.load() - txt_image.paste(char_image, (x, height - FONT_HEIGHT)) + int_image = Image.new("RGBA", txt_image.size, (0, 0, 0, 0)) + int_image.paste(char_image, (x, height - FONT_HEIGHT)) + txt_image = Image.alpha_composite(txt_image, int_image) txt_image = image_tint(txt_image, color) return txt_image