- Author:
- limodou
- Posted:
- February 25, 2007
- Language:
- Python
- Version:
- Pre .96
- Score:
- 22 (after 26 ratings)
Thumbnail an image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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' |
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
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_filename
isNone
by default, then that causes a problem ;)#
thanks to marioparris, I'v changed it.
#
I have been looking for somethink similar to this glad I found it
#
Please login first before commenting.