| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- coding: UTF-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (c) 2006 Tarek Ziadé |
|---|
| 5 | # |
|---|
| 6 | # Authors: |
|---|
| 7 | # Tarek Ziadé <tarek@ziade.org> |
|---|
| 8 | # |
|---|
| 9 | # This program is free software; you can redistribute it and/or |
|---|
| 10 | # modify it under the terms of the GNU General Public License |
|---|
| 11 | # as published by the Free Software Foundation; either version 2 |
|---|
| 12 | # of the License, or (at your option) any later version. |
|---|
| 13 | # |
|---|
| 14 | # This program is distributed in the hope that it will be useful, |
|---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | # GNU General Public License for more details. |
|---|
| 18 | # |
|---|
| 19 | # You should have received a copy of the GNU General Public License |
|---|
| 20 | # along with this program; if not, write to the Free Software |
|---|
| 21 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | # $Id: thumbnail.py 1655 2007-07-23 13:11:18Z rage $ |
|---|
| 23 | """Provide utilities to work with images""" |
|---|
| 24 | from PIL import Image |
|---|
| 25 | from StringIO import StringIO |
|---|
| 26 | |
|---|
| 27 | def _new_size(image, width, height, scale): |
|---|
| 28 | """calculate new size""" |
|---|
| 29 | if width == -1: |
|---|
| 30 | width = None |
|---|
| 31 | if height == -1: |
|---|
| 32 | height = None |
|---|
| 33 | img_width, img_height = image.size |
|---|
| 34 | if width is None and height is None and scale is None: |
|---|
| 35 | return image.size |
|---|
| 36 | |
|---|
| 37 | if width is not None and height is not None: |
|---|
| 38 | return width, height |
|---|
| 39 | |
|---|
| 40 | if width is not None or height is not None: |
|---|
| 41 | if width is not None: |
|---|
| 42 | ratio = float(img_width) / float(width) |
|---|
| 43 | height = float(img_height) / ratio |
|---|
| 44 | else: |
|---|
| 45 | ratio = float(img_height) / float(height) |
|---|
| 46 | width = float(img_width) / ratio |
|---|
| 47 | elif scale is not None: |
|---|
| 48 | height = img_height * scale |
|---|
| 49 | width = img_width * scale |
|---|
| 50 | |
|---|
| 51 | return int(width), int(height) |
|---|
| 52 | |
|---|
| 53 | def generate(filename, width=None, height=None, scale=None): |
|---|
| 54 | """returns a thumbnail""" |
|---|
| 55 | image = Image.open(filename) |
|---|
| 56 | width, height = _new_size(image, width, height, scale) |
|---|
| 57 | |
|---|
| 58 | #image.thumbnail((width, height), Image.ANTIALIAS) |
|---|
| 59 | #image.resize((width, height), Image.ANTIALIAS) |
|---|
| 60 | image.load() |
|---|
| 61 | image = image._new(image.im.stretch((width, height), Image.NEAREST)) |
|---|
| 62 | |
|---|
| 63 | data = StringIO() |
|---|
| 64 | image.save(data, "JPEG") |
|---|
| 65 | return data.getvalue() |
|---|
| 66 | |
|---|
| 67 | def image_size(file): |
|---|
| 68 | """returns image size""" |
|---|
| 69 | return Image.open(file).size |
|---|
| 70 | |
|---|