python图片大小归一化
时间: 2023-11-20 07:55:40 浏览: 107
用python 批量更改图像尺寸到统一大小的方法
以下是使用Python进行图片大小归一化的示例代码:
```python
from PIL import Image
def resize_image(input_image_path, output_image_path, size):
original_image = Image.open(input_image_path)
width, height = original_image.size
print(f"The original image size is {width} wide x {height} high")
resized_image = original_image.resize(size)
width, height = resized_image.size
print(f"The resized image size is {width} wide x {height} high")
resized_image.show()
resized_image.save(output_image_path)
if __name__ == '__main__':
resize_image('input_image.jpg', 'output_image.jpg', (800, 800))
```
在上述代码中,我们使用了Python的Pillow库来处理图片。`resize_image`函数接受三个参数:输入图片路径、输出图片路径和目标大小。函数首先打开输入图片,获取其原始大小,然后将其缩放到目标大小,并保存为输出图片。最后,函数会显示缩放后的图片。
阅读全文