Python Pillow 调整图像大小


大多数数字图像是像素的二维平面,它具有宽度和高度。Pillow库中的图像模块有一个属性大小。该元组包含图像的宽度和高度作为其元素。要调整图像大小,你可以通过指定宽度和高度来调用Pillow图像类的 resize() 方法。

调整大小并保存调整大小的图像


调整大小并保存调整大小的图像的程序如下:

#Import required Image library
from PIL import Image

#Create an Image Object from an Image
im = Image.open("images/cat.jpg")

#Display actual image
im.show()

#Make the new image half the width and half the height of the original image
resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))

#Display the resized imaged
resized_im.show()

#Save the cropped image
resized_im.save('resizedBeach1.jpg')

如果将上述程序保存为 例子.py 并执行,它将使用标准 PNG 显示实用程序显示原始图像和调整大小的图像,如下所示:

原始图像

Original Image

调整大小的图像

resized_image.jpg