Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non quadratic thumbnails: width, height, optional borders (code attached) #359

Open
bufemc opened this issue Jan 4, 2018 · 0 comments

Comments

@bufemc
Copy link

bufemc commented Jan 4, 2018

I would like to to generate non quadratic thumbnails, too. This could be done by specifying:

thumbnail width: (default: 200)
thumbnail height: (default: 200)
thumbnail crop: off/on (default: on)
thumbnail add borders (to fit exactly thumbnail size): off/on (default: off)

The default values just represent the guessed current behavior, but I would e.g. like to change to this:
width: 260
height: 195
crop: off
add borders: on

People like the author might prefer cropped pictures (as it is), but I prefer to see the full picture in the thumbnail already, even if there is need to add borders then to achieve same dimension for all images.

I also show some code how I did it so far, but it's in Python 3.6 and might serve just as template:
(on the other hand the thumbnail method used here is quick like hell, in tests it was fastest so far)

import os
from PIL import Image, ImageOps

THUMBS_PATH = 'f:/out/'
MAX_SIZE = (260,195) # 7x7
ADD_BORDERS = True

def create_thumbnail(in_path):
    im = Image.open(in_path)
    im.thumbnail(MAX_SIZE, Image.ANTIALIAS)
    out_path = THUMBS_PATH + os.path.basename(in_path)
    # Extend image with border if width or height smaller than MAX_SIZE
    if ADD_BORDERS:
        dw = MAX_SIZE[0] - im.size[0]
        dh = MAX_SIZE[1] - im.size[1]
        dw_half = int(dw / 2)
        dh_half = int(dh / 2)
        ltrb_border = (dw_half, dh_half, dw - dw_half, dh - dh_half)
        im_border = ImageOps.expand(im, border=ltrb_border, fill='black')
        im_border.save(out_path, 'JPEG')
    else:
        im.save(out_path, 'JPEG')

Additionally, but less important, I would like that you can also disable generating "web images" (the "thumbs" with _small suffix), instead just show the original picture (reason: hdd health).

@bufemc bufemc changed the title Possibility for non quadratic thumbnails: width, height, optional borders (code attached) Allow non quadratic thumbnails: width, height, optional borders (code attached) Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant