<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue. September 01 21:00:01 2020
A function
@author: hudanyunsheng
"""

import os
import cv2
import numpy as np
import copy
from plantcv.plantcv import fatal_error
from plantcv.plantcv import plot_image
from plantcv.plantcv import print_image
from plantcv.plantcv import params
import warnings


def _resize_img(img, new_size):
    """Resize the image to the given new size
    If the given size is larger than the original image size, zero-pad the image.
    If the given size is smaller than the origina image size, crop the image (right & bottom).
    """

    # original image size
    r_ori, c_ori = img.shape[0], img.shape[1]
    r, c = new_size[0], new_size[1]

    # check whether the input image is RGB or binary
    if len(img.shape) > 2:
        b_ori = np.shape(img)[2]
        junk = copy.deepcopy(img)
    else:
        b_ori = 1
        junk = np.expand_dims(img, axis=2)

    # deal with rows
    img1 = copy.deepcopy(junk)
    if r < r_ori:
        img1 = junk[0:r, :, :]
    elif r > r_ori:
        img1 = np.zeros_like(junk, shape=[r, c_ori, b_ori])
        img1[0:r_ori, :, :] = junk

    # update
    r_ori, c_ori = img1.shape[0], img1.shape[1]
    img2 = copy.deepcopy(img1)

    # deal with columns
    if c < c_ori:
        img2 = img1[:, 0:c, :]
    elif c > c_ori:
        img2 = np.zeros_like(img1, shape=[r_ori, c, b_ori])
        img2[:, 0:c_ori, :] = img1

    if b_ori == 1:
        output_img = np.squeeze(img2, axis=2)
    else:
        output_img = img2
    return output_img

def overlay_two_imgs(img1, img2, alpha=0.5, size_img=None):
    """
    A function used to overlay two images with a given alpha value (alpha indicated how opaque the 1st image is)
    Input grayscale images would be converted to RGB first
    inputs:
    img1: ndarray, can be either 3-dimensional (RGB image) or 2-dimensional (graysacle)
    img2: ndarray, can be either 3-dimensional (RGB image) or 2-dimensional (graysacle)
    alpha: desired opacity of 1st image, range: (0,1), default value=0.5
    size_img: desired size of the image, (width, height), default value=None (if there is no desired size, the output image size would be equal to the larger size of the two)
    """

    if alpha > 1 or alpha < 0:
        fatal_error("The value of alpha should be in the range of (0,1)!")

    ## check the dimensions of both images
    if len(img1.shape) == 3:
        img1_ = copy.deepcopy(img1)
    elif len(img1.shape) == 2:
        img1_ = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
    if len(img2.shape) == 3:
        img2_ = copy.deepcopy(img2)
    elif len(img2.shape) == 2:
        img2_ = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
<commit_msg>Update overlay_two_imgs.py<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue. September 01 21:00:01 2020
A function
@author: hudanyunsheng
"""

import os
import cv2
import numpy as np
import copy
from plantcv.plantcv import fatal_error
from plantcv.plantcv import plot_image
from plantcv.plantcv import print_image
from plantcv.plantcv import params
import warnings


def _resize_img(img, new_size):
    """Resize the image to the given new size

    Inputs:
    img         = RGB or grayscale image data
    new_size    = New dimensions of the output image


    Returns:
    output_img  = resized output image

    :param img: numpy.ndarray
    :param new_size: tuple
    :return output_img: numpy.ndarray

    Note:
    If the given size is larger than the original image size, zero-pad the image.
    If the given size is smaller than the original image size, crop the image (right & bottom).
    """

    # original image size
    r_ori, c_ori = img.shape[0], img.shape[1]
    r, c = new_size[0], new_size[1]

    # check whether the input image is RGB or binary
    if len(img.shape) > 2:
        b_ori = np.shape(img)[2]
        junk = copy.deepcopy(img)
    else:
        b_ori = 1
        junk = np.expand_dims(img, axis=2)

    # deal with rows
    img1 = copy.deepcopy(junk)
    if r < r_ori:
        img1 = junk[0:r, :, :]
    elif r > r_ori:
        img1 = np.zeros_like(junk, shape=[r, c_ori, b_ori])
        img1[0:r_ori, :, :] = junk

    # update
    r_ori, c_ori = img1.shape[0], img1.shape[1]
    img2 = copy.deepcopy(img1)

    # deal with columns
    if c < c_ori:
        img2 = img1[:, 0:c, :]
    elif c > c_ori:
        img2 = np.zeros_like(img1, shape=[r_ori, c, b_ori])
        img2[:, 0:c_ori, :] = img1

    if b_ori == 1:
        output_img = np.squeeze(img2, axis=2)
    else:
        output_img = img2
    return output_img

def overlay_two_imgs(img1, img2, alpha=0.5, size_img=None):
    """
    A function used to overlay two images with a given alpha value (alpha indicated how opaque the 1st image is)
    Input grayscale images would be converted to RGB first

    Inputs:
    img1        = ndarray, can be either 3-dimensional (RGB image) or 2-dimensional (graysacle)
    img2        = ndarray, can be either 3-dimensional (RGB image) or 2-dimensional (graysacle)
    alpha       = desired opacity of 1st image, range: (0,1), default value=0.5
    size_img    = desired size of the image, (width, height), default value=None (if there is no desired size, the
                  output image size would be equal to the larger size of the two)


    Returns:
    out_img     = blended output image

    :param img1: numpy.ndarray
    :param img2: numpy.ndarray
    :param alpha: float
    :param size_img: tuple
    :return out_img: numpy.ndarray
    """

    if alpha > 1 or alpha < 0:
        fatal_error("The value of alpha should be in the range of (0,1)!")

    ## check the dimensions of both images
    if len(img1.shape) == 3:
        img1_ = copy.deepcopy(img1)
    elif len(img1.shape) == 2:
        img1_ = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
    if len(img2.shape) == 3:
        img2_ = copy.deepcopy(img2)
    elif len(img2.shape) == 2:
        img2_ = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)

{'fraction_non_alphanumeric': 0.10219707057256991, 'fraction_numerical': 0.02996005326231691, 'mean_word_length': 2.693300553165335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2757773', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2068, 'n_words': 684}