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 <fraggle@gmail.com>
Signed-off-by: RjY <rjy@users.sourceforge.net>
This commit is contained in:
RjY 2013-12-12 08:28:32 +00:00
parent 2b49e3eabc
commit 30464a1204

View file

@ -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)
try:
command_line = [
CONVERT_COMMAND,
'-background', bgcolor,
tmp,
'-flatten',
output_filename
]
command_line.extend([
'-background', bgcolor, '-flatten',
output_filename])
invoke_command(command_line)
finally:
remove(tmp)
def generate_graphics(graphics, color=COLOR_WHITE):
for name, text in sorted(graphics.items()):