拍爱

thumbnail_utils.py 502B

12345678910111213141516171819
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. try:
  4. from PIL import Image
  5. except ImportError:
  6. import Image
  7. def make_thumbnail(im_path, im_thumbnail_path=None, max_width=360):
  8. im = Image.open(im_path)
  9. width, height = im.size
  10. thumb_width = min(max_width, width)
  11. thumb_height = height / width * thumb_width
  12. im.thumbnail((thumb_width, thumb_height))
  13. im.save(im_thumbnail_path or im_path, im.format or 'JPEG')
  14. return width, height, thumb_width, thumb_height