Python 如何查看文件夹中有多少png文件及其分辨率,以便打印到excel?
我的文件夹结构如下Python 如何查看文件夹中有多少png文件及其分辨率,以便打印到excel?,python,Python,我的文件夹结构如下 数据集文件夹 子文件夹1 a、 png b.png c.png 子文件夹2 a、 png b.png c.png 子文件夹3 a、 png b.png c.png 子文件夹4 a、 png b.png c.png 子文件夹n a、 png b.png c.png 您可以使用递归查找文件,但我使用和str.endswith(“.png”)收集所有子目录中的所有png文件 使用优秀的模块获得图像的分辨率 导入操作系统 从集合导入defaultdict 从P
- 子文件夹1
- a、 png b.png c.png
- 子文件夹2
- a、 png b.png c.png
- 子文件夹3
- a、 png b.png c.png
- 子文件夹4
- a、 png b.png c.png
- 子文件夹n
- a、 png b.png c.png
您可以使用递归查找文件,但我使用和
str.endswith(“.png”)
收集所有子目录中的所有png文件
使用优秀的模块获得图像的分辨率
导入操作系统
从集合导入defaultdict
从PIL导入图像
DIR=r“C:\Dataset文件夹”
结果=defaultdict(列表)
对于根目录,请使用os.walk(DIR)中的文件名:
subdirectory=root.replace(f“{DIR}\\”,“”)
对于文件名中的文件名:
如果filename.endswith(“.png”):
filepath=os.path.join(根目录,文件名)
宽度,高度=Image.open(filepath).size
结果[子目录].append((文件名、宽度、高度))
#打印输出
对于子目录,results.items()中的图像:
打印(f“{子目录}有{len(图像)}图像:”)
对于图像中的文件名、宽度和高度:
打印(f“{filename}分辨率为{width}x{height}”)
打印()
#手动写入CSV文件
打开(“png files.csv”、“wt”)作为f:
f、 写入(“子目录、文件名、宽度、高度\n”)#标题
对于子目录,results.items()中的图像:
对于图像中的文件名、宽度和高度:
f、 写入(f{subdirectory}、{filename}、{width}、{height}\n“)#行
stdout
输出:
Subfolder1 has 4 image(s):
a.png resolution is 640x480
b.png resolution is 640x480
c.png resolution is 300x300
d.png resolution is 300x300
Subfolder2 has 4 image(s):
a.png resolution is 300x300
b.png resolution is 300x300
c.png resolution is 300x300
d.png resolution is 300x300
Subfolder3 has 4 image(s):
a.png resolution is 300x300
b.png resolution is 300x300
c.png resolution is 26x26
d.png resolution is 300x30
Subfolder4 has 1 image(s):
a.png resolution is 256x240
CSV文件:
Subdirectory,Filename,Width,Height
Subfolder1,a.png,640,480
Subfolder1,b.png,640,480
Subfolder1,c.png,300,300
Subfolder1,d.png,300,300
Subfolder2,a.png,300,300
Subfolder2,b.png,300,300
Subfolder2,c.png,300,300
Subfolder2,d.png,300,300
Subfolder3,a.png,300,300
Subfolder3,b.png,300,300
Subfolder3,c.png,26,26
Subfolder3,d.png,300,30
Subfolder4,a.png,256,240
使用Microsoft Excel打开的CSV文件:
我运行代码时没有出错。但是当我试图打印
stdout
时,我得到了NameError
import os
from collections import defaultdict
from PIL import Image
import pandas as pd
DIR = r"C:\Users\Me\Desktop\works\project\ai\tenPercent" # my subfolders are in the tenPercent folder
results = defaultdict(list)
for root, _, filenames in os.walk(DIR):
subdirectory = root.replace(f"{DIR}\\", "")
print(filenames)
print(root)
for filename in filenames:
if filename.endswith(".png"):
filepath = os.path.join(root, filename)
width, height = Image.open(filepath).size
results[subdirectory].append((filename, width, height))
for subdirectory, images in results.items():
print(f"{subdirectory} has {len(images)} image(s):")
for filename, width, height in images:
print(f"{filename} resolution is {width}x{height}")
print()
with open("png_files.csv", "wt") as f:
f.write("Subdirectory,Filename,Width,Height\n") # Header
for subdirectory, images in results.items():
for filename, width, height in images:
f.write(f"{subdirectory},{filename},{width},{height}\n") # Rows
你需要明确你想要实现什么。所以您需要所有这些文件夹中的
*.png
文件的计数及其分辨率。“用于打印到excel”是什么意思?与Microsoft Excel一样,电子表格应用程序?你想用Python将这些图像放到电子表格中吗?我想像这样输出;子文件夹3有3个图像--a.png分辨率为200x300 b.png分辨率为200x300。。。。然后我想用这个输出创建一个csv非常感谢你的帮助。我将文件夹路径更改为我的,但csv为空,尽管路径正确。我不明白我在哪里犯了错误。我也不明白那是怎么发生的。因此,输出到stdout很好,但是没有写入CSV文件?有什么错误吗?你能发布代码让我看看吗?stdout
不是一个变量,它是控制台/IDE打印到的缓冲区的名称。您遇到了什么问题?我确实使用该路径运行了代码,没有出现错误,但csv返回空。而且我没有像你那样得到任何输出。奇怪。csv文件是否有标题:子目录、文件名、宽度、高度?将print(“Results:,Results)
放在之前的行上,用于子目录,Results.items()中的图像:
。现在的输出是什么?是的,我可以看到标题。当我尝试将结果的输出作为回报时<代码>结果:defaultdict(,{})在任何目录中都找不到任何.png
图像。在这一行下方if filename.endswith(“.png”):
putprint(“filename:”,filename)
。输出是什么?