utilityfun.py
def overlay_image(img, overlay_img, opacity=1.0):
    """Reliably combine two images based on the opacity. Black pixels are
    treated as transparent. The method also takes in a grayscale base image.
    """
    if len(img.shape) < 3:
        img = np.stack((img, img, img), axis=-1)
    if len(overlay_img.shape) < 3:
        overlay_img = np.stack((overlay_img, overlay_img, overlay_img), axis=-1)

    img_out = np.zeros(img.shape)
    overlay_img = (overlay_img * opacity).astype(int)
    faded_img = (img * (1 - opacity)).astype(int)

    for i in range(0, img.shape[2]):
        channel_out = np.where(overlay_img[:, :, i] == 0,
                               img[:, :, i],
                               faded_img[:, :, i] + overlay_img[:, :, i])

        img_out[:, :, i] = np.where(channel_out > 255, 255, channel_out)

    return img_out.astype('uint8')
Reliably combine two images based on the opacity. Black pixels are
    treated as transparent. The method also takes in a grayscale base image.
    
Reliably combine two images based on the opacity. Black pixels are
treated as transparent. The method also takes in a grayscale base image.
Reliably combine two images based on the opacity . Black pixels are treated as transparent . The method also takes in a grayscale base image .
def overlay_image(img, overlay_img, opacity=1.0):
    if len(img.shape) < 3:
        img = np.stack((img, img, img), axis=-1)
    if len(overlay_img.shape) < 3:
        overlay_img = np.stack((overlay_img, overlay_img, overlay_img), axis=-1)
    img_out = np.zeros(img.shape)
    overlay_img = (overlay_img * opacity).astype(int)
    faded_img = (img * (1 - opacity)).astype(int)
    for i in range(0, img.shape[2]):
        channel_out = np.where(overlay_img[:, :, i] == 0,
                               img[:, :, i],
                               faded_img[:, :, i] + overlay_img[:, :, i])
        img_out[:, :, i] = np.where(channel_out > 255, 255, channel_out)
    return img_out.astype('uint8')
def overlay_image ( img , overlay_img , opacity = 1.0 ) : if len ( img . shape ) < 3 : img = np . stack ( ( img , img , img ) , axis = - 1 ) if len ( overlay_img . shape ) < 3 : overlay_img = np . stack ( ( overlay_img , overlay_img , overlay_img ) , axis = - 1 ) img_out = np . zeros ( img . shape ) overlay_img = ( overlay_img * opacity ) . astype ( int ) faded_img = ( img * ( 1 - opacity ) ) . astype ( int ) for i in range ( 0 , img . shape [ 2 ] ) : channel_out = np . where ( overlay_img [ : , : , i ] == 0 , img [ : , : , i ] , faded_img [ : , : , i ] + overlay_img [ : , : , i ] ) img_out [ : , : , i ] = np . where ( channel_out > 255 , 255 , channel_out ) return img_out . astype ( 'uint8' )
Reliably combine two images based on the opacity.
Reliably combine two images based on the opacity .
"""Reliably combine two images based on the opacity. Black pixels are
    treated as transparent. The method also takes in a grayscale base image.
    """
img
overlay_img
opacity