12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- from __future__ import division
- try:
- from cStringIO import StringIO
- from cStringIO import StringIO as BytesIO
- except ImportError:
- try:
- from StringIO import StringIO
- from StringIO import StringIO as BytesIO
- except ImportError:
- from io import BytesIO, StringIO
- try:
- from PIL import Image
- except ImportError:
- import Image
- def make_thumbnail(im_path, im_thumbnail_path=None, max_width=360):
- im = Image.open(im_path)
- width, height = im.size
- thumb_width = min(max_width, width)
- thumb_height = height / width * thumb_width
- im.thumbnail((thumb_width, thumb_height), Image.ANTIALIAS)
- im.save(im_thumbnail_path or im_path, im.format or 'JPEG', quality=90)
- return width, height, thumb_width, thumb_height
- def make_thumbnail2(data, w=360, h=240):
- im = Image.open(BytesIO(data))
- fmt = im.format.lower()
- width, height = im.size
- if width <= w and height <= h:
- return data
- if width > height:
- thumb_width, thumb_height = w, height * w / width
- else:
- thumb_width, thumb_height = width * h / height, h
- im.thumbnail((thumb_width, thumb_height), Image.ANTIALIAS)
- out = BytesIO()
- im.save(out, format=fmt)
- data = out.getvalue()
- return data
|