在Python中调整图像大小

问题描述 投票:-2回答:2

我想调整格式460x700 rgb的图像大小。但是当我启动我的功能时,一些图像的格式为168x256x3,而其他图像的格式为166x256x3。

def process_image(image):

    size = 256, 256
    image.thumbnail(size, Image.ANTIALIAS)
    #image = image.crop((128 - 112, 128 - 112, 128 + 112, 128 + 112))
    npImage = np.array(image)
    npImage = npImage/255.

    return npImage

我希望所有图像都具有相同的大小。

python image image-resizing numpy-ndarray
2个回答
0
投票

缩略图功能保留纵横比。您应该使用调整大小功能

image.resize(size, Image.ANTIALIAS)

0
投票

您可以使用.resize()来调整图像大小:

image.resize((256, 256), Image.ANTIALIAS) 

第一个数字(256)是以像素为单位的宽度。

第二个数字(256)是以像素为单位的高度。

ANTIALIAS是一种高质量的下采样滤波器


你的新代码:

def process_image(image):

    size = 256, 256
    image.thumbnail(size, Image.ANTIALIAS)
    #image = image.crop((128 - 112, 128 - 112, 128 + 112, 128 + 112))

    image.resize((256, 256), Image.ANTIALIAS) 
    #resizes the image and gives it width and height of 256 pixels  

    npImage = np.array(image)
    npImage = npImage/255.

    return npImage
© www.soinside.com 2019 - 2024. All rights reserved.