Resize image maintaining aspect ratio AND making portrait and landscape images exact same size?
Asked Answered
L

3

12

Currently I am using:

    os.chdir(album.path)
    images = glob.glob('*.*')

    # thumbs size
    size = 80,80

    for image in images:
        #create thumb
        file, ext = os.path.splitext(image)
        im = Image.open(os.path.join(album.path,image))
        im.thumbnail(size, Image.ANTIALIAS)
        thumb_path = os.path.join(album.path, 'thumbs', file + ".thumb" + ".jpeg")
        im.save(thumb_path)

Although this works, I end up with different sizes images (some are portrait and some are landscape), but I want all of the images to have an exact size. Maybe a sensible cropping?

UPDATE:

I don't mind cropping a small portion of the image. When I said sensible cropping I mean something like this algorythm:

if image is portrait:
    make width 80px
    crop the height (will be more than 80px)
else if image is landscape:
    make height 80px
    crop the width to 80px (will be more than 80px)
Louella answered 1/2, 2012 at 20:58 Comment(5)
Consider reading the manuals. The thumnail method is meant to preserve the aspect of the image. Obviously, you cannot have both, as not all images have the same aspect ratio. Consider using resize if you want to rescale without preserving the ratio.Incorporable
He could make them the same size if he wanted to do a fit, which would pad out the extra area with a constant color. That would allow a 4x3 or a 16x9 or any aspect image to fit within a given dimension of 80x80Messina
Unfortunately there's no standard way to define "sensible" for cropping. It sounds like you want to always crop a square from the image, which means you risk cutting off parts you really wanted to keep.Lengthy
@MarkRansom - I agree. If your source image happens to have a very non-square aspect you will risk losing much of the image. I think a fit padding will be best. See my answer.Messina
Thanks for the answers guys, but I prefer to cut the images and lose a portion of them instead of adding padding. See my update above.Louella
M
19

Here is my take on doing a padded fit for an image:

#!/usr/bin/env python

from PIL import Image, ImageChops

F_IN = "/path/to/image_in.jpg"
F_OUT = "/path/to/image_out.jpg"

size = (80,80)

image = Image.open(F_IN)
image.thumbnail(size, Image.ANTIALIAS)
image_size = image.size

thumb = image.crop( (0, 0, size[0], size[1]) )

offset_x = max( (size[0] - image_size[0]) / 2, 0 )
offset_y = max( (size[1] - image_size[1]) / 2, 0 )

thumb = ImageChops.offset(thumb, offset_x, offset_y)
thumb.save(F_OUT)

It first uses the thumbnail operation to bring the image down to within your original bounds and preserving the aspect. Then it crops it back out to actually fill the size of your bounds (since unless the original image was square, it will be smaller now), and we find the proper offset to center the image. The image is offset to the center, so you end up with black padding but no image cropping.

Unless you can make a really sensible guess at a proper center crop without losing possible important image data on the edges, a padded fit approach will work better.

Update

Here is a version that can do either center crop or pad fit.

#!/usr/bin/env python

from PIL import Image, ImageChops, ImageOps

def makeThumb(f_in, f_out, size=(80,80), pad=False):

    image = Image.open(f_in)
    image.thumbnail(size, Image.ANTIALIAS)
    image_size = image.size

    if pad:
        thumb = image.crop( (0, 0, size[0], size[1]) )

        offset_x = max( (size[0] - image_size[0]) / 2, 0 )
        offset_y = max( (size[1] - image_size[1]) / 2, 0 )

        thumb = ImageChops.offset(thumb, offset_x, offset_y)

    else:
        thumb = ImageOps.fit(image, size, Image.ANTIALIAS, (0.5, 0.5))

    thumb.save(f_out)


source = "/path/to/source/image.JPG"

makeThumb(source, "/path/to/source/image_padded.JPG", pad=True)
makeThumb(source, "/path/to/source/image_centerCropped.JPG", pad=False)
Messina answered 1/2, 2012 at 21:42 Comment(1)
This is really close to what I need @jdi, thanks! How do i alter it to chop and not insert the padding? (even losing a chunk of the image)Louella
V
2

Obviously, you would need to crop or pad the images. You could do something like below to get a maximal centered crop according to the aspect ratio of the thumbnails (untested):

aspect = lambda size: float(size[0]) / float(size[1])
sa = aspect(size)
if aspect(im.size) > sa:
    width = int(sa * im.size[1])
    left = (im.size[0] - width) / 2
    im = im.crop((left, 0, left + width, im.size[1]))
else:
    height = int(im.size[0] / sa)
    top = (im.size[1] - height) / 2
    im = im.crop((0, top, im.size[0], top + height))
im.thumbnail(size, Image.ANTIALIAS)
Variance answered 1/2, 2012 at 21:12 Comment(1)
This example has a few bugs in it. im.crop() doesn't crop in place. It needs to return the image object to itself. The arguments to crop need to be a box tuple, so you need to wrap those in (). You are passing floats to the crop. Need to do int(width) and int(height). Also, all of this is not necessary as there is a built in method: im = ImageOps.fit(im, size, Image.ANTIALIAS, (0.5, 0.5)) that does the same thing.Messina
O
0

If you use easy-thumbnails you'll need to set crop to True and upscale to True to always fill-up the space (have the exact same dimensions).

Ex: makes image_2 fits in image_1 dimensions:

    thumbnailer = get_thumbnailer(image_2)
    thumbnail = thumbnailer.generate_thumbnail(thumbnail_options={
        'crop': True,
        'upscale': True,
        'size': image_1.size
    })
    image_2 = thumbnail.image
Overcloud answered 6/8, 2015 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.