From 88175f02c7ed69c9134c05e3c28c4f126808c3cf Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 7 Aug 2012 00:15:17 +0200 Subject: [PATCH] use ImageOps.fit to create the thumbnails cropped to thumb_size, if the thumb_fit params is True (replace the square_thumb param). --- README.rst | 4 ++-- sigal/image.py | 29 ++++++++--------------------- sigal/settings.py | 2 +- tests/sample/sigal.conf | 4 ++-- 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/README.rst b/README.rst index fa86348..744ef85 100644 --- a/README.rst +++ b/README.rst @@ -56,11 +56,11 @@ directory `input_dir/sigal.conf`. thumb_prefix = TN- # thumbnail size thumb_size = 150x112 + # crop the image to fill the box + thumb_fit = 1 # keep big image big_img = 1 bigimg_dir = original - # generate squared thumbnails - square_thumb = 1 # jpeg quality jpg_quality = 90 # keep exif metadatas in output image diff --git a/sigal/image.py b/sigal/image.py index 322a4ba..5d552bd 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -25,7 +25,7 @@ Prepare images: resize images, and create thumbnails with some options import os from PIL import Image as PILImage -from PIL import ImageDraw as PILImageDraw +from PIL import ImageDraw, ImageOps from shutil import copy2 DESCRIPTION_FILE = "album_description" @@ -53,30 +53,17 @@ class Image: def add_copyright(self, text): "add copyright to image" - draw = PILImageDraw.Draw(self.img) + draw = ImageDraw.Draw(self.img) draw.text((5, self.img.size[1] - 15), '\xa9 ' + text) - def thumbnail(self, filename, size, square=False, quality=90): - "create thumbnail image for img" + def thumbnail(self, filename, box, fit=True, quality=90): + "Create a thumbnail image" - nx, ny = self.img.size - - if square: - if nx > ny: - offset = (nx - ny) / 2 - box = (offset, 0, nx - offset, ny) - else: - offset = (ny - nx) / 2 - box = (0, offset, nx, ny - offset) - - self.img = self.img.crop(box) - thumb_size = [size[0], size[0]] - elif nx > ny: - thumb_size = size + if fit: + self.img = ImageOps.fit(self.img, box, PILImage.ANTIALIAS) else: - thumb_size = [size[1], size[0]] + self.img.thumbnail(box, PILImage.ANTIALIAS) - self.img.thumbnail(thumb_size, PILImage.ANTIALIAS) self.img.save(filename, quality=quality) @@ -166,7 +153,7 @@ class Gallery: img.save(im_name, quality=self.settings['jpg_quality']) img.thumbnail(thumb_name, self.settings['thumb_size'], - square=self.settings['square_thumb'], + fit=self.settings['thumb_fit'], quality=self.settings['jpg_quality']) if self.settings['exif']: diff --git a/sigal/settings.py b/sigal/settings.py index 0c2e20a..bcb3b00 100644 --- a/sigal/settings.py +++ b/sigal/settings.py @@ -25,7 +25,7 @@ _DEFAULT_CONFIG = { 'thumb_prefix': '', 'thumb_size': '150x112', 'thumb_dir': 'thumbnail', - 'square_thumb': 0, + 'thumb_fit': 1, 'big_img': 0, 'bigimg_dir': 'big', 'jpg_quality': 90, diff --git a/tests/sample/sigal.conf b/tests/sample/sigal.conf index 38321dd..79cfcc6 100644 --- a/tests/sample/sigal.conf +++ b/tests/sample/sigal.conf @@ -8,11 +8,11 @@ img_size = 640x480 thumb_prefix = TN- # thumbnail size thumb_size = 200x150 +# crop the image to fill the box +thumb_fit = 1 # keep big image big_img = 1 bigimg_dir = original -# generate squared thumbnails -square_thumb = 1 # jpeg quality jpg_quality = 90 # keep exif metadatas in output image