Grouping images by specified number