<commit_before>import os
import logging

from PIL import Image as ImageFile, ImageFont, ImageDraw


log = logging.getLogger(__name__)

# TODO: move to a fonts store
FONT = os.path.normpath(os.path.join(
    os.path.dirname(__file__), os.pardir, os.pardir,
    'data', 'fonts', 'Impact.ttf'
))


class Image:
    """JPEG generated by applying text to a template."""

    def __init__(self, template, text, root=None):
        self.template = template
        self.text = text
        self.root = root

    @property
    def path(self):
        if self.root:
            return os.path.join(self.root, self.template.key,
                                self.text.path + '.jpg')
        else:
            return None

    def generate(self):
        directory = os.path.dirname(self.path)
        if not os.path.isdir(directory):
            os.makedirs(directory)
        make_meme(self.text.top, self.text.bottom,
                  self.template.path, self.path)


# based on: https://github.com/danieldiekmeier/memegenerator
def make_meme(top, bottom, background, path, match_font_size=True):
    """Add text to an image and save it."""
    img = ImageFile.open(background)

    # Resize to a maximum height and width
    img.thumbnail((500, 500))
    image_size = img.size

    # Draw image
    draw = ImageDraw.Draw(img)

    max_font_size = int(image_size[1] / 5)
    min_font_size = int(image_size[1] / 17)
    max_text_len = image_size[0] - 20
    top_font_size, top = _optimize_font_size(top, max_font_size,
                                        min_font_size, max_text_len)
    bottom_font_size, bottom = _optimize_font_size(bottom, max_font_size,
                                        min_font_size, max_text_len)

    if match_font_size is True:
        top_font_size = min(top_font_size, bottom_font_size)
        bottom_font_size = top_font_size

    top_font = ImageFont.truetype(FONT, top_font_size)
    bottom_font = ImageFont.truetype(FONT, bottom_font_size)

    top_text_size = draw.multiline_textsize(top, top_font)
    bottom_text_size = draw.multiline_textsize(bottom, bottom_font)<commit_msg>pep8 appeasement<commit_after>import os
import logging

from PIL import Image as ImageFile, ImageFont, ImageDraw


log = logging.getLogger(__name__)

# TODO: move to a fonts store
FONT = os.path.normpath(os.path.join(
    os.path.dirname(__file__), os.pardir, os.pardir,
    'data', 'fonts', 'Impact.ttf'
))


class Image:
    """JPEG generated by applying text to a template."""

    def __init__(self, template, text, root=None):
        self.template = template
        self.text = text
        self.root = root

    @property
    def path(self):
        if self.root:
            return os.path.join(self.root, self.template.key,
                                self.text.path + '.jpg')
        else:
            return None

    def generate(self):
        directory = os.path.dirname(self.path)
        if not os.path.isdir(directory):
            os.makedirs(directory)
        make_meme(self.text.top, self.text.bottom,
                  self.template.path, self.path)


# based on: https://github.com/danieldiekmeier/memegenerator
def make_meme(top, bottom, background, path, match_font_size=True):
    """Add text to an image and save it."""
    img = ImageFile.open(background)

    # Resize to a maximum height and width
    img.thumbnail((500, 500))
    image_size = img.size

    # Draw image
    draw = ImageDraw.Draw(img)

    max_font_size = int(image_size[1] / 5)
    min_font_size = int(image_size[1] / 17)
    max_text_len = image_size[0] - 20
    top_font_size, top = _optimize_font_size(top, max_font_size,
                                             min_font_size, max_text_len)
    bottom_font_size, bottom = _optimize_font_size(bottom, max_font_size,
                                                   min_font_size, max_text_len)

    if match_font_size is True:
        top_font_size = min(top_font_size, bottom_font_size)
        bottom_font_size = top_font_size

    top_font = ImageFont.truetype(FONT, top_font_size)
    bottom_font = ImageFont.truetype(FONT, bottom_font_size)

    top_text_size = draw.multiline_textsize(top, top_font)
    bottom_text_size = draw.multiline_textsize(bottom, bottom_font)
{'fraction_non_alphanumeric': 0.09734305196332001, 'fraction_numerical': 0.006818716200329179, 'mean_word_length': 2.5509181969949917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323407', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1344, 'n_words': 300}