Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 634
avlnx Avatar asked Feb 01 '12 20:02

avlnx


People also ask

What does maintaining aspect ratio mean when resizing images?

When you resize images, most of the time you want to maintain aspect ratio. Maintaining aspect ratio is also known as "constraining proportions" in some graphic editing software (like Photoshop). It basically means that the width and height of the resized picture is enlarged/shrunk to proportion,...

How to resize an image instead of cropping it?

In the Crop Image section, select one of the available ratios from the Aspect Ratio menu or add a custom one. Then click the Resize image to aspect ratio option to resize the image instead of cropping it. How to center the image using transparent margins?

How do I keep the same aspect ratio in Photoshop?

Most image editors have a “constrain proportions” checkbox that automatically does this for you, or you can usually hold down the Shift key while resizing. Keeping the same aspect ratio makes sure that your image doesn’t look stretched or warped.

How do I keep the same ratio of width to height?

When you resize an image or screenshot, keep the same ratio of width to height. Most image editors have a “constrain proportions” checkbox that automatically does this for you, or you can usually hold down the Shift key while resizing.


1 Answers

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)
like image 127
jdi Avatar answered Sep 21 '22 07:09

jdi