Discussions>Resize image keeping the ratio of width and height unchanged>

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?

3 votesJW270.00
1 Answers

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
            new_h, new_w = size, size * w / h
        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
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?