import traceback


def sort_images(images):
    """ Sorts an image by its shape

    Images is a dictionary of {key : { 'img': np.array }} at input.
    For each image the shape is is used to compute a cluster id. This id is
    appended to the image sub-dictionary.
    """

    for imgName in images.keys():
        try:
            img = images[imgName]['img']
            shapeDirName = 'shape_' + str(img.shape[0]) + 'x' + str(img.shape[1]) + 'x' + str(img.shape[2])
            images[imgName]['cluster'] = shapeDirName
        except Exception as e:
            print(e)
            traceback.print_exc()
    return images