import random


def img_get_random_patch(img,w,h):
    """Get a random patch of a specific width and height from an image"""

    # Note that for this function it is the user's responsibility to ensure
    # the image size is big enough. We'll do an asertion to help but...

    # Figure out the maximum starting point within the image that 
    max_x = img.shape[1] - w
    max_y = img.shape[0] - h

    # Make sure the size is big enough
    assert max_x >= 0, 'Trying to get a patch wider that the image width'
    assert max_y >= 0, 'Trying to get a patch higher that the image height'

    # Get a random starting point
    x = random.randint(0,max_x)
    y = random.randint(0,max_y)

    # Get the patch within the image
    image_patch = img[y:y+h,x:x+w, ...]

    # All done
    return image_patch