import math


def template_number2image(template, number):
    """Construct the an image from the template and image number."""

    length = template.count("#")

    # check that the number will fit in the template

    if (math.pow(10, length) - 1) < number:
        raise RuntimeError("number too big for template")

    format = "%%0%dd" % length

    return template.replace("#" * length, format % number)