From a63ff79a716f3119ff1e2b35c87f5d9a7798b76b Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Tue, 14 May 2013 23:41:15 +0200 Subject: [PATCH] Add a setting to choose the pilkit processor used to resize the images. --- sigal/gallery.py | 3 ++- sigal/image.py | 23 ++++++++++++++++------- sigal/settings.py | 1 + sigal/templates/sigal.conf.py | 7 +++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/sigal/gallery.py b/sigal/gallery.py index 1bec011..734c906 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -238,7 +238,8 @@ def process_image(filepath, outpath, settings): shutil.copy(filepath, join(outpath, settings['orig_dir'], filename)) generate_image(filepath, outname, settings['img_size'], None, - options=options, copyright_text=settings['copyright']) + options=options, copyright_text=settings['copyright'], + method=settings['img_processor']) if settings['make_thumbs']: thumb_name = join(outpath, get_thumb(settings, filename)) diff --git a/sigal/image.py b/sigal/image.py index 4d4896e..c50d518 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -21,14 +21,17 @@ # IN THE SOFTWARE. import logging +import pilkit.processors +import sys + from PIL import Image as PILImage from PIL import ImageDraw, ImageOps -from pilkit.processors import ProcessorPipeline, Transpose, ResizeToFill +from pilkit.processors import Transpose from pilkit.utils import save_image def generate_image(source, outname, size, format, options=None, - autoconvert=True, copyright_text=''): + autoconvert=True, copyright_text='', method='ResizeToFit'): """Image processor, rotate and resize the image. :param source: path to an image @@ -42,14 +45,20 @@ def generate_image(source, outname, size, format, options=None, # Rotate the img, and catch IOError when PIL fails to read EXIF try: - processor = Transpose() - img = processor.process(img) + img = Transpose().process(img) except IOError: pass - # Run the other processors - processors = [ResizeToFill(*size, upscale=False)] - img = ProcessorPipeline(processors).process(img) + # Resize the image + try: + logger.debug('Processor: %s', method) + processor_cls = getattr(pilkit.processors, method) + except AttributeError as e: + logger.error('Wrong processor name: %s', method) + sys.exit() + + processor = processor_cls(*size, upscale=False) + img = processor.process(img) if copyright_text: add_copyright(img, copyright_text) diff --git a/sigal/settings.py b/sigal/settings.py index 943920b..d03375a 100644 --- a/sigal/settings.py +++ b/sigal/settings.py @@ -27,6 +27,7 @@ _DEFAULT_CONFIG = { 'source': '', 'destination': '_build', 'img_size': (640, 480), + 'img_processor': 'ResizeToFit', 'make_thumbs': True, 'thumb_prefix': '', 'thumb_suffix': '', diff --git a/sigal/templates/sigal.conf.py b/sigal/templates/sigal.conf.py index 7e93e54..1c6b50d 100644 --- a/sigal/templates/sigal.conf.py +++ b/sigal/templates/sigal.conf.py @@ -19,6 +19,13 @@ theme = 'galleria' # Size of resized image img_size = (800, 600) +# Pilkit processor used to resize the image +# (see http://pilkit.readthedocs.org/en/latest/#processors) +# - ResizeToFit: fit the image within the specified dimensions (default) +# - ResizeToFill: crop THE IMAGE it to the exact specified width and height +# - SmartResize: identical to ResizeToFill, but uses entropy to crop the image +# img_processor = 'ResizeToFit' + # Generate thumbnails # make_thumbs = True