1. A method comprising: by a computing device, accessing a plurality of image elements that have an ordered sequence; by the computing device, determining a plurality of clusters of the image elements, each of the clusters comprising one or more of the image elements arranged in a line, the image elements within each cluster being scaled to have a substantially same first dimension while maintaining their original aspect ratios and being arranged to maintain the ordered sequence, and each of the clusters having a substantially same second dimension; by the computing device, determining a cost for each of the clusters based on a cost function; by the computing device, accessing a graph based on the clusters that maintains the ordered sequence, the graph comprising a plurality of paths that each represent a layout of the image elements and have a total cost based on the costs of the clusters along the path; and by the computing device, identifying the path with a lowest total cost to determine a preferred one of the layouts.