'Generate a Kindle masthead image.
Generate a Kindle masthead image, used with Kindle periodical format.
Args:
out_path (str): path to write generated masthead image
Input:
opts.catalog_title (str): Title to render
masthead_font: User-specified font preference (MOBI output option)
Output:
out_path (file): masthead image (GIF)'

def generate_masthead_image(self, out_path):

    from calibre.ebooks.conversion.config import load_defaults
    MI_WIDTH = 600
    MI_HEIGHT = 60
    font_path = default_font = P('fonts/liberation/LiberationSerif-Bold.ttf')
    recs = load_defaults('mobi_output')
    masthead_font_family = recs.get('masthead_font', 'Default')
    if (masthead_font_family != 'Default'):
        from calibre.utils.fonts.scanner import font_scanner
        faces = font_scanner.fonts_for_family(masthead_font_family)
        if faces:
            font_path = faces[0]['path']
    if ((not font_path) or (not os.access(font_path, os.R_OK))):
        font_path = default_font
    try:
        from PIL import Image, ImageDraw, ImageFont
        (Image, ImageDraw, ImageFont)
    except ImportError:
        import Image, ImageDraw, ImageFont
    img = Image.new('RGB', (MI_WIDTH, MI_HEIGHT), 'white')
    draw = ImageDraw.Draw(img)
    try:
        font = ImageFont.truetype(font_path, 48)
    except:
        self.opts.log.error(("                    Failed    to    load    user-specifed    font    '%s'" % font_path))
        font = ImageFont.truetype(default_font, 48)
    text = self.opts.catalog_title.encode('utf-8')
    (width, height) = draw.textsize(text, font=font)
    left = max(int(((MI_WIDTH - width) / 2.0)), 0)
    top = max(int(((MI_HEIGHT - height) / 2.0)), 0)
    draw.text((left, top), text, fill=(0, 0, 0), font=font)
    img.save(open(out_path, 'wb'), 'GIF')
