要使用Python调整图片大小,你可以使用Pillow库,它是PIL库的一个分支,专门用于Python 3。以下是使用Pillow库调整图片大小的步骤和示例代码:
1. 安装Pillow库:
```bash
pip install pillow
2. 导入Pillow库:
```python
from PIL import Image
3. 打开图片文件:
```python
img = Image.open('input_image.jpg') 替换为你的图片路径
4. 设置新的尺寸:
```python
new_size = (800, 600) 替换为你想要的宽度和高度
5. 调整图片大小:
```python
resized_img = img.resize(new_size, Image.ANTIALIAS) 使用抗锯齿选项以获得更好的质量
6. 保存调整后的图片:
```python
resized_img.save('resized_image.jpg') 替换为你想要保存的新图片路径
完整的示例代码如下:
```python
from PIL import Image
def resize_image(input_path, output_path, new_width, new_height):
img = Image.open(input_path)
img_resized = img.resize((new_width, new_height), Image.ANTIALIAS)
img_resized.save(output_path)
使用示例
resize_image('input_image.jpg', 'resized_image.jpg', 800, 600)
请确保将`input_path`和`output_path`替换为你的实际图片路径,以及将`new_width`和`new_height`替换为你想要设置的新尺寸。