帮我用python代码检索指定文件夹,找出所有的图像文件(如jpg文件),并读取显示
时间: 2024-05-03 14:21:03 浏览: 81
非常好,请看下面的代码。您可以将文件夹路径指定为变量,以便在不同的文件夹中运行代码。
```python
import os
from PIL import Image
# 指定要搜索图像的文件夹路径
folder_path = "指定文件夹的路径"
# 遍历文件夹中所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.jpg'): # 如果是图像文件
file_path = os.path.join(folder_path, filename)
with Image.open(file_path) as img:
img.show() # 显示图像
```
希望这有帮助!
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)