import math


def paste_image(image, canvas, position):
    """
    Pastes the given image on the canvas at the given position. The position denotes the center of the pasted image.
    """
    x_offset = int(math.floor(position[0] - (image.shape[1] / 2)))
    y_offset = int(math.floor(position[1] - (image.shape[0] / 2)))

    pasted_part_start_x = max(0, x_offset * -1)
    pasted_part_start_y = max(0, y_offset * -1)
    pasted_part_end_x = min(image.shape[1], canvas.shape[1] - x_offset)
    pasted_part_end_y = min(image.shape[0], canvas.shape[0] - y_offset)

    pasted_part = image[pasted_part_start_y:pasted_part_end_y, pasted_part_start_x:pasted_part_end_x]

    b_start_x = max(0, x_offset)
    b_start_y = max(0, y_offset)

    canvas[b_start_y:b_start_y+pasted_part.shape[0], b_start_x:b_start_x+pasted_part.shape[1]] = pasted_part
    return canvas