def calculate_image_size(size, target_size):
    """
    Returns the size of the image after resizing.
    The same code is used in PIL.Image.thumbnail
    The results of this function is used by JavaScript
    in resize effect while changing image.
    """
    x, y = size
    # if the width is greater than the target
    if x > target_size[0]:
        # proportionally decrease the height but not less than 1px
        y = int(max(y * target_size[0] / x, 1))
        x = int(target_size[0])  # set the width to the target width
    # if the height is still greater than the target
    if y > target_size[1]:
        # proportionally decrease the width but not less than 1px
        x = int(max(x * target_size[1] / y, 1))
        y = int(target_size[1])  # set the height to the target height
    return x, y