怎么批量读取图片路径输出宽高
时间: 2024-05-01 20:18:39 浏览: 18
您可以使用Python的Pillow库来读取图片路径和输出宽高信息。以下是一个示例代码:
```python
from PIL import Image
import os
# 文件夹路径
folder_path = 'folder'
# 获取文件夹中所有文件
file_list = os.listdir(folder_path)
# 遍历所有图片文件
for file_name in file_list:
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path) and (file_name.lower().endswith('.jpg') or file_name.lower().endswith('.png')):
with Image.open(file_path) as img:
width, height = img.size
print("{} width: {}, height: {}".format(file_name, width, height))
```
该代码将扫描'folder'文件夹中的所有图片文件,并打印每个文件的文件名、宽度和高度。
相关问题
怎么批量读取图片路径输出宽高 输出成单个
文件?
你可以使用Python中的PIL库来批量读取图片路径并输出它们的宽高。以下是Python代码示例:
```
from PIL import Image
import os
# 设置图片文件夹路径
folder_path = 'image_folder/'
# 遍历文件夹中所有图片文件,并输出它们的宽高
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
file_path = os.path.join(folder_path, filename)
with Image.open(file_path) as img:
width, height = img.size
print(f'{filename}: {width} x {height}')
```
这段代码将遍历指定文件夹中的所有.jpg、.jpeg和.png文件,并输出它们的宽高。你可以将输出保存到一个单独的文件中,或者将它们输出到控制台。
怎么批量读取图片路径输出 width 的数值 和 height的数值
您可以使用Python中的Pillow库来实现。具体操作如下所示:
1. 首先,你需要导入Pillow库:
```
from PIL import Image
```
2. 然后,你可以使用Image打开图片并获取其尺寸:
```
image = Image.open("image.jpg")
width, height = image.size
print("Width: ", width)
print("Height: ", height)
```
这样就可以打印出图片的宽度和高度了。
另外,你要提醒你的用户:批量读取图片路径的方法可以使用os模块和列表推导式实现。
相关推荐
![m](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)