From 30464a1204ee047c98a35ccc483aa08fc8ec5895 Mon Sep 17 00:00:00 2001 From: RjY Date: Thu, 12 Dec 2013 08:28:32 +0000 Subject: [PATCH] graphics/text: run ImageMagick only once per generated image - factor out command line generation: a new function _make_command_line returns most of the command line for a transparent image rendering. - Callers render_transparent_text and render_text append the output filename and extra imagemagick options to colour the background as needed. This means imagemagick is only called once per generated image, which makes the process considerably faster. - This also works around a bug in the current version of imagemagick in Debian sid, which caused single-character graphics (e.g. winum*) to be generated with a black background. DW: http://www.doomworld.com/vb/post/1221810 Acked-by: Simon Howard Signed-off-by: RjY --- graphics/text/textgen | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/graphics/text/textgen b/graphics/text/textgen index 3e10c625..0fa1109e 100755 --- a/graphics/text/textgen +++ b/graphics/text/textgen @@ -35,9 +35,7 @@ # from glob import glob -from os import remove from sys import stdout -from tempfile import mktemp import subprocess import re @@ -171,11 +169,8 @@ class Font(object): if c is None: return x - def render_transparent_text(self, text, output_filename, color): - """Render the given text to the given output file. - - The rendered text is generated with a transparent background. - """ + def _make_command_line(self, text, color): + """Command line construction helper, used in render functions""" width = self.text_width(text) command_line = [ @@ -196,31 +191,33 @@ class Font(object): command_line.extend([ '-colorize', '%i,%i,%i' % color, - output_filename ]) + return command_line + + def render_transparent_text(self, text, output_filename, color): + """Render the given text to the given output file. + + The rendered text is generated with a transparent background. + """ + + command_line = self._make_command_line(text, color) + + command_line.append(output_filename) + invoke_command(command_line) def render_text(self, text, output_filename, color=COLOR_WHITE, bgcolor=BACKGROUND_COLOR): """Render text to a file with the given background color.""" - tmp = mktemp('.gif') + command_line = self._make_command_line(text, color) - self.render_transparent_text(text, tmp, color) + command_line.extend([ + '-background', bgcolor, '-flatten', + output_filename]) - try: - command_line = [ - CONVERT_COMMAND, - '-background', bgcolor, - tmp, - '-flatten', - output_filename - ] - - invoke_command(command_line) - finally: - remove(tmp) + invoke_command(command_line) def generate_graphics(graphics, color=COLOR_WHITE): for name, text in sorted(graphics.items()):