Resize image keeping the ratio of width and height unchanged

Guys I'm using tensorflow and trying to train a classifier for some predefined classes.I want to resize images with keeping the ration of width and height. Do you have any implementations?

pythonresize-image
3 votesJW234.00
1 Answers
JO220.00
2

Just use this function, which resizes the image by it's greater side.

from PIL import Image

def resize_image(img, size):
    w, h = img.size

    if isinstance(size, int):
        if h < w:
            new_h, new_w = size * h / w, size
        else:
            new_h, new_w = size, size * w / h
    else:
        new_h, new_w = size

    new_h, new_w = int(new_h), int(new_w)

    new_image = img.resize((new_w, new_h), Image.BILINEAR)
    return new_image
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?