def do_overlay(img, overlay_path, overlay_source=None, overlay_tint=None, overlay_size=None, overlay_position=None):
    if not overlay_path:
        return img

    if overlay_source == 'media':
        overlay = pil.open(MEDIA_STORAGE.open(overlay_path))
    else:
        overlay = pil.open(STATIC_STORAGE.open(overlay_path))

    # We want the overlay to fit in the image
    iw, ih = img.size
    ow, oh = overlay.size
    overlay_ratio = float(ow) / float(oh)

    if overlay_size:
        tw, th = overlay_size.split(',')
        ow = int(round(float(tw.strip()) * iw))
        oh = int(round(float(th.strip()) * ih))
        if ow < 0:
            ow = oh * overlay_ratio
        elif oh < 0:
            oh = ow / overlay_ratio

        overlay = resizeScale(overlay, ow, oh, overlay_source + "/" + overlay_path)
        ow, oh = overlay.size
    else:
        have_to_scale = False
        if ow > iw:
            ow = iw
            oh = int(float(iw) / overlay_ratio)
            have_to_scale = True
        if oh > ih:
            ow = int(float(ih) * overlay_ratio)
            oh = ih
            have_to_scale = True

        if have_to_scale:
            overlay = resizeScale(overlay, ow, oh, overlay_source + "/" + overlay_path)
            ow, oh = overlay.size

    if overlay_tint:
        do_tint(overlay, overlay_tint)

    if not overlay_position:
        target_x = int((iw - ow) / 2)
        target_y = int((ih - oh) / 2)
    else:
        tx, ty = overlay_position.split(',')
        if tx == "":
            target_x = int((iw - ow) / 2)
        else:
            target_x  = int(round(float(tx.strip()) * iw))
        if ty == "":
            target_y = int((ih - oh) / 2)
        else:
            target_y  = int(round(float(ty.strip()) * ih))

    """
    TODO: paste seems to be buggy, because pasting over opaque background returns a non opaque image
    (the parts that are not 100% opaque or 100% transparent become partially transparent. 
    the putalpha workareound doesn't seem to look nice enough
    """
    #r, g, b, a = img.split()
    #img.paste(overlay, (int((iw - ow) / 2), int((ih - oh) / 2)), overlay)
    #img.putalpha(a)
    do_paste(img, overlay, (target_x, target_y))

    return img
dataset/ETHPy150Open francescortiz/image/image/utils.py/do_overlay/original