Skip to content

Commit

Permalink
Add config option for tweaking how thumbnails should be cropped
Browse files Browse the repository at this point in the history
  • Loading branch information
3onyc committed Oct 1, 2017
1 parent 838d94e commit d2f58da
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sigal/image.py
Expand Up @@ -131,15 +131,15 @@ def generate_image(source, outname, settings, options=None):
save_image(img, outname, outformat, options=options, autoconvert=True)


def generate_thumbnail(source, outname, box, delay, fit=True, options=None):
def generate_thumbnail(source, outname, box, delay, fit=True, options=None, thumb_fit_centering=(0.5, 0.5)):
"""Create a thumbnail image."""

logger = logging.getLogger(__name__)
img = _read_image(source)
original_format = img.format

if fit:
img = ImageOps.fit(img, box, PILImage.ANTIALIAS)
img = ImageOps.fit(img, box, PILImage.ANTIALIAS, centering=thumb_fit_centering)
else:
img.thumbnail(box, PILImage.ANTIALIAS)

Expand Down Expand Up @@ -171,7 +171,8 @@ def process_image(filepath, outpath, settings):
thumb_name = os.path.join(outpath, get_thumb(settings, filename))
generate_thumbnail(outname, thumb_name, settings['thumb_size'],
settings['thumb_video_delay'],
fit=settings['thumb_fit'], options=options)
fit=settings['thumb_fit'], options=options,
thumb_fit_centering=settings["thumb_fit_centering"])
except Exception as e:
logger.info('Failed to process: %r', e)
if logger.getEffectiveLevel() == logging.DEBUG:
Expand Down
1 change: 1 addition & 0 deletions sigal/settings.py
Expand Up @@ -66,6 +66,7 @@
'theme': 'colorbox',
'thumb_dir': 'thumbnails',
'thumb_fit': True,
'thumb_fit_centering': (0.5, 0.5),
'thumb_prefix': '',
'thumb_size': (200, 150),
'thumb_suffix': '',
Expand Down
5 changes: 5 additions & 0 deletions sigal/templates/sigal.conf.py
Expand Up @@ -93,6 +93,11 @@
# Crop the image to fill the box
# thumb_fit = True

# When using thumb_fit, specifies what we should crop
# for usage see
# http://pillow.readthedocs.io/en/3.1.x/reference/ImageOps.html#PIL.ImageOps.fit
# thumb_fit_centering = (0.5, 0.5)

# Delay in seconds to avoid black thumbnails in videos with fade-in
# thumb_video_delay = '0'

Expand Down

0 comments on commit d2f58da

Please sign in to comment.