高度固定为100,把不同大小照片等比例缩放 python
时间: 2024-03-13 15:48:02 浏览: 55
您好!针对您的问题,您可以通过使用Python中的Pillow库来实现对照片的等比例缩放。具体实现过程如下:
1. 安装Pillow库
您可以使用以下命令在终端或命令行中安装Pillow库:
```
pip install Pillow
```
2. 编写Python代码
您可以使用以下代码对照片进行等比例缩放,同时保持高度固定为100:
```python
from PIL import Image
def resize_image_with_fixed_height(image_path, output_path, fixed_height):
with Image.open(image_path) as image:
# 计算缩放比例
height_percent = (fixed_height / float(image.size[1]))
width_size = int((float(image.size[0]) * float(height_percent)))
# 缩放图片
resized_image = image.resize((width_size, fixed_height))
# 保存图片
resized_image.save(output_path)
```
接下来,您可以调用该函数并传入照片路径、输出路径以及固定高度参数,来实现对照片的等比例缩放:
```python
resize_image_with_fixed_height('path/to/input/image', 'path/to/output/image', 100)
```
上述代码中,第一个参数为输入照片的路径,第二个参数为输出照片的路径,第三个参数为固定高度。该函数会自动计算缩放比例,并将缩放后的照片保存到输出路径中。
希望上述代码可以帮助您解决问题,如有疑问请随时追问。
阅读全文
相关推荐

















