예제 #1
0
파일: utils.py 프로젝트: kamrul500/image
def do_mask(img, mask_path, mask_source, mask_mode=None):
    if not mask_path:
        return img

    if mask_source == 'media':
        mask = pil.open(MEDIA_STORAGE.open(mask_path)).convert("RGBA")
    else:
        mask = pil.open(STATIC_STORAGE.open(mask_path)).convert("RGBA")

    # We want the mask to have the same size than the image
    if mask_mode == 'distort':
        iw, ih = img.size
        mw, mh = mask.size
        if mw != iw or mh != ih:
            mask = mask.resize((iw, ih), pil.ANTIALIAS)

    else:
        # We want the overlay to fit in the image
        iw, ih = img.size
        ow, oh = mask.size
        overlay_ratio = float(ow) / float(oh)
        have_to_scale = False
        if ow > iw:
            ow = iw
            oh = int(float(iw) / overlay_ratio)
        if oh > ih:
            ow = int(float(ih) * overlay_ratio)
            oh = ih

        if ow != iw or oh != ih:
            have_to_scale = True

        if have_to_scale:
            nmask = mask.resize((ow, oh), pil.ANTIALIAS)
            mask = pil.new('RGBA', (iw, ih))
            # mask.paste(nmask, (int((iw - ow) / 2), int((ih - oh) / 2)), nmask)
            mask = do_paste(mask, nmask, (int((iw - ow) / 2), int(
                (ih - oh) / 2)))
            ow, oh = mask.size

    r, g, b, a = mask.split()
    img.putalpha(a)
예제 #2
0
def do_mask(img, mask_path, mask_source, mask_mode=None):
    if not mask_path:
        return img

    if mask_source == 'media':
        mask = pil.open(MEDIA_STORAGE.open(mask_path)).convert("RGBA")
    else:
        mask = pil.open(STATIC_STORAGE.open(mask_path)).convert("RGBA")

    # We want the mask to have the same size than the image
    if mask_mode == 'distort':
        iw, ih = img.size
        mw, mh = mask.size
        if mw != iw or mh != ih:
            mask = mask.resize((iw, ih), pil.ANTIALIAS)

    else:
        # We want the overlay to fit in the image
        iw, ih = img.size
        ow, oh = mask.size
        overlay_ratio = float(ow) / float(oh)
        have_to_scale = False
        if ow > iw:
            ow = iw
            oh = int(float(iw) / overlay_ratio)
        if oh > ih:
            ow = int(float(ih) * overlay_ratio)
            oh = ih

        if ow != iw or oh != ih:
            have_to_scale = True

        if have_to_scale:
            nmask = mask.resize((ow, oh), pil.ANTIALIAS)
            mask = pil.new('RGBA', (iw, ih))
            #mask.paste(nmask, (int((iw - ow) / 2), int((ih - oh) / 2)), nmask)
            do_paste(mask, nmask, (int((iw - ow) / 2), int((ih - oh) / 2)))
            ow, oh = mask.size

    r, g, b, a = mask.split()
    img.putalpha(a)
예제 #3
0
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
예제 #4
0
파일: utils.py 프로젝트: kamrul500/image
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(',')
        tx = tx.strip()
        ty = ty.strip()

        if tx == "":
            # Center horizontally.
            target_x = int((iw - ow) / 2)
        else:
            if "!" in tx:
                # X origin on the right side.
                x_percent = 1.0 - float(tx.replace("!", "").strip())
                target_x = int(round(x_percent * iw) - ow)
            else:
                # X origin on the left side.
                x_percent = float(tx)
                target_x = int(round(x_percent * iw))

        if ty == "":
            # Center vertically.
            target_y = int((ih - oh) / 2)
        else:
            if "!" in ty:
                # Y origin on the bottom side.
                y_percent = 1.0 - float(ty.replace("!", "").strip())
                target_y = int(round(y_percent * ih) - oh)
            else:
                # Y origin on the top side.
                y_percent = float(ty)
                target_y = int(round(y_percent * 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
    """
    img = do_paste(img, overlay, (target_x, target_y))

    return img
예제 #5
0
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(',')
        tx = tx.strip()
        ty = ty.strip()

        if tx == "":
            # Center horizontally.
            target_x = int((iw - ow) / 2)
        else:
            if "!" in tx:
                # X origin on the right side.
                x_percent = 1.0 - float(tx.replace("!", "").strip())
                target_x = int(round(x_percent * iw) - ow)
            else:
                # X origin on the left side.
                x_percent = float(tx)
                target_x = int(round(x_percent * iw))

        if ty == "":
            # Center vertically.
            target_y = int((ih - oh) / 2)
        else:
            if "!" in ty:
                # Y origin on the bottom side.
                y_percent = 1.0 - float(ty.replace("!", "").strip())
                target_y = int(round(y_percent * ih) - oh)
            else:
                # Y origin on the top side.
                y_percent = float(ty)
                target_y = int(round(y_percent * 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
    """
    img = do_paste(img, overlay, (target_x, target_y))

    return img