Commit 1cb58f08 authored by fred's avatar fred
Browse files

fix thumbnailing to non squares

parent f210ddce
......@@ -31,11 +31,20 @@ def thumbnail(image, size='100x100'):
if abs( (1.0*x/y) - (1.0*image.size[0]/image.size[1]) ) > 0.1:
# aspect ratio change, crop the image first
if image.size[0] > image.size[1]: # landscape
box = [0, 0, int(image.size[1] * (1.0*x/y)), image.size[1]]
box = [0, 0, int(image.size[1] * (1.0*y/x)), image.size[1]]
else:
box = [0, 0, image.size[0], int(image.size[0] * (1.0*y/x))]
if box[2] > image.size[0]:
box = [int(t*(1.0*image.size[0]/box[2])) for t in box]
if box[3] > image.size[1]:
box = [int(t*(1.0*image.size[1]/box[3])) for t in box]
if image.size[0] > image.size[1]: # landscape
box[0] = (image.size[0] - box[2]) / 2 # keep the middle
else:
box = [0, 0, image.size[0], int(image.size[0] * (1.0*x/y))]
box[1] = (image.size[1] - box[3]) / 4 # keep mostly the top
image = image.crop(box)
image = image.resize([x, y], Image.ANTIALIAS)
try:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment