def blit_source_image(output, template, image, panel):
    """
    Blit the source image to the output image, scaling and cropping as needed.
    """
    img = Image.open(image)
    screen = TEMPLATES[template]['screen']
    factor = float(screen[0]) / float(img.size[0])
    dimensions = [int(i * factor) for i in img.size]
    if panel:
        dimensions[1] -= TEMPLATES[template]['panel']
    img = img.resize(dimensions, Image.ANTIALIAS)
    img = img.crop([0, 0] + [min(*i) for i in zip(dimensions, screen)])
    offset = list(TEMPLATES[template]['offset'])
    if panel:
        offset[1] += TEMPLATES[template]['panel']
    output.paste(img, tuple(offset))
dataset/ETHPy150Open 2buntu/2buntu-blog/twobuntu/touch/generator.py/blit_source_image/original