用Python将文件夹下的全部图片修改成一样尺寸
时间: 2024-04-13 15:29:32 浏览: 17
你可以使用Python的PIL库来处理图片,并将它们调整为相同的尺寸。下面是一个示例代码:
```python
from PIL import Image
import os
def resize_images(folder_path, target_size):
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.png'): # 可根据需要修改图片格式
image_path = os.path.join(folder_path, filename)
with Image.open(image_path) as image:
resized_image = image.resize(target_size)
resized_image.save(image_path)
# 示例用法
folder_path = 'your_folder_path' # 替换为你的文件夹路径
target_size = (500, 500) # 替换为目标尺寸
resize_images(folder_path, target_size)
```
在上面的示例中,你需要将`your_folder_path`替换为你的文件夹路径,`target_size`替换为你想要的目标尺寸。这段代码会遍历文件夹下的所有图片,并将它们调整为指定的尺寸。请确保你已经安装了PIL库,可以使用`pip install pillow`命令进行安装。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)