Add config option for tweaking how thumbnails should be cropped

This commit is contained in:
3onyc
2017-10-01 11:11:04 +02:00
parent 838d94eb61
commit f32e4f1729
4 changed files with 11 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
Sigal is written and maintained by Simon Conseil and contributors (in
alphabetical order):
- 3onyc
- Abdul Qabiz
- Alexandre Chataignon (@xouillet)
- Alexey Bazhin

View File

@@ -131,7 +131,7 @@ 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__)
@@ -139,7 +139,7 @@ def generate_thumbnail(source, outname, box, delay, fit=True, options=None):
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)
@@ -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:

View File

@@ -66,6 +66,7 @@ _DEFAULT_CONFIG = {
'theme': 'colorbox',
'thumb_dir': 'thumbnails',
'thumb_fit': True,
'thumb_fit_centering': (0.5, 0.5),
'thumb_prefix': '',
'thumb_size': (200, 150),
'thumb_suffix': '',

View File

@@ -93,6 +93,11 @@ thumb_size = (280, 210)
# 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'