Skip to content

Instantly share code, notes, and snippets.

@MyGodIsHe
Last active December 16, 2015 19:09
Show Gist options
  • Save MyGodIsHe/5482770 to your computer and use it in GitHub Desktop.
Save MyGodIsHe/5482770 to your computer and use it in GitHub Desktop.
from sorl.thumbnail import default
def sort_images_by_col(object_list, col_count, width, margin, key):
"""
return [[col width, objects], ...]
"""
def get_col():
min = cols[0]
min_col = 0
for i in cols:
if i[0] < min:
min = i[0]
min_col = i
return min_col
cols = [[0, []] for i in range(col_count)]
for obj in object_list:
col = get_col()
image = default.engine.get_image(key(obj))
w, h = default.engine.get_image_size(image)
col[0] += width * h / w + margin
col[1].append(obj)
for i in cols:
if i[0]:
i[0] -= margin
heights = [i[0] for i in cols if i[0]]
average = (max(heights) + min(heights)) / 2
for i in cols:
if i[0]:
i[0] = str(int(width * average / i[0]))
return cols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment