from PIL import Image
import os.path
import StringIO
def thumbnail(filename, size=(50, 50), output_filename=None):
image = Image.open(filename)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image = image.resize(size, Image.ANTIALIAS)
# get the thumbnail data in memory.
if not output_filename:
output_filename = get_default_thumbnail_filename(filename)
image.save(output_filename, image.format)
return output_filename
def thumbnail_string(buf, size=(50, 50)):
f = StringIO.StringIO(buf)
image = Image.open(f)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image = image.resize(size, Image.ANTIALIAS)
o = StringIO.StringIO()
image.save(o, "JPEG")
return o.getvalue()
def get_default_thumbnail_filename(filename):
path, ext = os.path.splitext(filename)
return path + '.thumb.jpg'
Comments
See also: http://trac.studioquattro.biz/djangoutils/wiki/Thumbnail
#
To avoid extra copies, I'd recommend changing
to
The
thing is also unnecessary; save() takes either a filename or a file object, so there's no need to open it yourself.
Cheers /F
#
thanks
#
Also, change line 13 from:
output_filename = get_default_thumbnail_filename(output_filename)to:
output_filename = get_default_thumbnail_filename(filename)so it uses the filename of the file you are thumbnailing. If
output_filenameisNoneby default, then that causes a problem ;)#
thanks to marioparris, I'v changed it.
#
To avoid extra copies, I'd recommend changing
image = Image.open(filename) image = image.convert('RGB') image = image.resize(size, Image.ANTIALIAS)
Thanks
BlackBerry Curve 9320 cases
#