def thumbnail_from_big_image(big_image, size=None):
try:
src_w, src_h = get_image_dimensions(big_image)
img = Image.open(big_image)
img.thumbnail((512,512))
th_w, th_h = img.size
if size is None:
size = settings.DEFAULT_MAX_THUMB_SIZE
if th_w > size or th_h > size:
x = (th_w - size)/2.0
if x < 0:
x = 0
y = (th_h - size)/2.0
if y < 0:
y = 0
img = img.crop((x,y, size+x, size+y))
output = StringIO()
img.save(output, format="PNG")
contents = output.getvalue()
output.close()
return ContentFile(contents)
except IOError:
return None
评论列表
文章目录