-
Notifications
You must be signed in to change notification settings - Fork 6
/
图片文件夹或ziprar文件内的图片类型高度宽度大小信息生成.txt(拖拽,图片文件夹或ziprar).py
263 lines (251 loc) · 16.3 KB
/
图片文件夹或ziprar文件内的图片类型高度宽度大小信息生成.txt(拖拽,图片文件夹或ziprar).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
# pip install pillow
from PIL import Image
from pathlib import Path
import sys
import datetime
import zipfile
import rarfile
def formatFileSize(sizeBytes):
sizeBytes = float(sizeBytes)
result = float(abs(sizeBytes))
suffix = "B";
if(result > 1024):
suffix = "KB"
mult = 1024
result = result / 1024
if(result > 1024):
suffix = "MB"
mult *= 1024
result = result / 1024
if (result > 1024) :
suffix = "GB"
mult *= 1024
result = result / 1024
if (result > 1024) :
suffix = "TB"
mult *= 1024
result = result / 1000
if (result > 1024) :
suffix = "PB"
mult *= 1024
result = result / 1024
return format(result,'.2f') + suffix
def writeFile(myPath, filereadlines):
newfile = open(myPath, mode='a+', encoding='UTF-8')
newfile.writelines(filereadlines)
newfile.close()
def main(inputPath):
del inputPath[0]
for aPath in inputPath:
# 文件格式
fileType = ['.jpg', '.png']
# 图片宽度统计,size[0]
fileWidth = {}
# 图片高度统计,size[1]
fileHeight = {}
# 图片长度宽度统计,[size[0],,size[1]]
fileWidthAndHeight = {}
# 图片类型统计
numberOfFileType = {}
# 文件夹大小
allFileSize = 0
# 文件夹文件数量
allFileCount = 0
# 输出
outputFile = []
# 压缩文件信息输出到一个文件 = True,各自的文件 = False
outputToOneFile = True
if Path.is_file(Path(aPath)):
if rarfile.is_rarfile(Path(aPath)):
with rarfile.RarFile(Path(aPath)) as rf:
fileNameList = []
for eachFile in rf.infolist():
if not eachFile.is_dir():
fileNameList.append(eachFile.filename)
allFileCount = len(fileNameList)
for fileName in fileNameList:
if Path(fileName).suffix.lower() in fileType:
with rf.open(fileName) as readImage:
tmpImg = Image.open(readImage)
if Path(fileName).suffix not in numberOfFileType:
numberOfFileType[Path(fileName).suffix] = 1
else:
numberOfFileType[Path(fileName).suffix] = numberOfFileType[Path(fileName).suffix] + 1
if (tmpImg.size[1] not in fileHeight):
fileHeight[tmpImg.size[1]] = 1
elif (tmpImg.size[1] in fileHeight):
fileHeight[tmpImg.size[1]] = fileHeight[tmpImg.size[1]] + 1
if (tmpImg.size[0] not in fileWidth):
fileWidth[tmpImg.size[0]] = 1
elif (tmpImg.size[0] in fileWidth):
fileWidth[tmpImg.size[0]] = fileWidth[tmpImg.size[0]] + 1
if ((tmpImg.size[0],tmpImg.size[1]) not in fileWidthAndHeight):
fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] = 1
elif ((tmpImg.size[0],tmpImg.size[1]) in fileWidthAndHeight):
fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] = fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] + 1
outputFile.append(Path(aPath).name + '\n')
for key in sorted(numberOfFileType):
print('类型:' + key[1:] + ',数量: ' + str(numberOfFileType[key]))
outputFile.append('类型:' + key[1:] + ',数量: ' + str(numberOfFileType[key]) + '\n')
for key in fileHeight:
print('高度:' + str(key) + ', 数量: ' + str(fileHeight[key]))
outputFile.append('高度:' + str(key) + ', 数量: ' + str(fileHeight[key]) + '\n')
for key in fileWidth:
print('宽度:' + str(key) + ', 数量: ' + str(fileWidth[key]))
outputFile.append('宽度:' + str(key) + ', 数量: ' + str(fileWidth[key]) + '\n')
for key in fileWidthAndHeight:
print('宽度和高度:' + str(key) + ', 数量: ' + str(fileWidthAndHeight[key]))
outputFile.append('宽度和高度:' + str(key) + ', 数量: ' + str(fileWidthAndHeight[key]) + '\n')
print('文件数量:' + str(allFileCount))
print('文件大小:' + formatFileSize(Path(aPath).stat().st_size))
print('文件创建时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_ctime).strftime('%Y-%m-%d %H:%M:%S'))
print('文件修改时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S'))
outputFile.append('文件数量:' + str(allFileCount) + '\n')
outputFile.append('文件大小:' + formatFileSize(Path(aPath).stat().st_size) + '\n')
outputFile.append('文件创建时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_ctime).strftime('%Y-%m-%d %H:%M:%S') + '\n')
outputFile.append('文件修改时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S') + '\n\n')
if outputToOneFile:
writeFile(Path(aPath).parent.joinpath(Path(aPath).parent.name + '.txt'), outputFile)
else:
writeFile(Path(aPath).parent.joinpath(Path(aPath).name + '.txt'), outputFile)
if zipfile.is_zipfile(Path(aPath)):
with zipfile.ZipFile(Path(aPath)) as zf:
fileNameList = []
for eachFile in zf.infolist():
if not eachFile.is_dir():
fileNameList.append(eachFile.filename)
allFileCount = len(fileNameList)
for fileName in fileNameList:
if Path(fileName).suffix.lower() in fileType:
with zf.open(fileName) as readImage:
tmpImg = Image.open(readImage)
if Path(fileName).suffix not in numberOfFileType:
numberOfFileType[Path(fileName).suffix] = 1
else:
numberOfFileType[Path(fileName).suffix] = numberOfFileType[Path(fileName).suffix] + 1
if (tmpImg.size[1] not in fileHeight):
fileHeight[tmpImg.size[1]] = 1
elif (tmpImg.size[1] in fileHeight):
fileHeight[tmpImg.size[1]] = fileHeight[tmpImg.size[1]] + 1
if (tmpImg.size[0] not in fileWidth):
fileWidth[tmpImg.size[0]] = 1
elif (tmpImg.size[0] in fileWidth):
fileWidth[tmpImg.size[0]] = fileWidth[tmpImg.size[0]] + 1
if ((tmpImg.size[0],tmpImg.size[1]) not in fileWidthAndHeight):
fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] = 1
elif ((tmpImg.size[0],tmpImg.size[1]) in fileWidthAndHeight):
fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] = fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] + 1
outputFile.append(Path(aPath).name + '\n')
for key in sorted(numberOfFileType):
print('类型:' + key[1:] + ',数量: ' + str(numberOfFileType[key]))
outputFile.append('类型:' + key[1:] + ',数量: ' + str(numberOfFileType[key]) + '\n')
for key in fileHeight:
print('高度:' + str(key) + ', 数量: ' + str(fileHeight[key]))
outputFile.append('高度:' + str(key) + ', 数量: ' + str(fileHeight[key]) + '\n')
for key in fileWidth:
print('宽度:' + str(key) + ', 数量: ' + str(fileWidth[key]))
outputFile.append('宽度:' + str(key) + ', 数量: ' + str(fileWidth[key]) + '\n')
for key in fileWidthAndHeight:
print('宽度和高度:' + str(key) + ', 数量: ' + str(fileWidthAndHeight[key]))
outputFile.append('宽度和高度:' + str(key) + ', 数量: ' + str(fileWidthAndHeight[key]) + '\n')
print('文件数量:' + str(allFileCount))
print('文件大小:' + formatFileSize(Path(aPath).stat().st_size))
print('文件创建时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_ctime).strftime('%Y-%m-%d %H:%M:%S'))
print('文件修改时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S'))
outputFile.append('文件数量:' + str(allFileCount) + '\n')
outputFile.append('文件大小:' + formatFileSize(Path(aPath).stat().st_size) + '\n')
outputFile.append('文件创建时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_ctime).strftime('%Y-%m-%d %H:%M:%S') + '\n')
outputFile.append('文件修改时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S') + '\n\n')
if outputToOneFile:
writeFile(Path(aPath).parent.joinpath(Path(aPath).parent.name + '.txt'), outputFile)
else:
writeFile(Path(aPath).parent.joinpath(Path(aPath).name + '.txt'), outputFile)
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('**/*'):
if Path.is_file(file) and (file.suffix in fileType):
allFileSize = allFileSize + file.stat().st_size
allFileCount = allFileCount + 1
tmpImg = Image.open(file)
if file.suffix not in numberOfFileType:
numberOfFileType[file.suffix] = 1
else:
numberOfFileType[file.suffix] = numberOfFileType[file.suffix] + 1
if (tmpImg.size[1] not in fileHeight):
fileHeight[tmpImg.size[1]] = 1
elif (tmpImg.size[1] in fileHeight):
fileHeight[tmpImg.size[1]] = fileHeight[tmpImg.size[1]] + 1
if (tmpImg.size[0] not in fileWidth):
fileWidth[tmpImg.size[0]] = 1
elif (tmpImg.size[0] in fileWidth):
fileWidth[tmpImg.size[0]] = fileWidth[tmpImg.size[0]] + 1
if ((tmpImg.size[0],tmpImg.size[1]) not in fileWidthAndHeight):
fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] = 1
elif ((tmpImg.size[0],tmpImg.size[1]) in fileWidthAndHeight):
fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] = fileWidthAndHeight[(tmpImg.size[0],tmpImg.size[1])] + 1
folderName = Path(aPath).name
if folderName[0] == '[':
letterCount = 0
for eachLetter in folderName:
if eachLetter in ['[', ']']:
letterCount = letterCount + 1
# 文件夹格式[书名][作者][出版]Vol01
if letterCount == 6:
folderName = folderName
print('书名:' + folderName.split('][')[0][1:])
print('作者:' + folderName.split('][')[1])
outputFile.append('书名:' + folderName.split('][')[0][1:] + '\n')
outputFile.append('作者:' + folderName.split('][')[1] + '\n')
if folderName[-1] != ']' and folderName.split('][')[2].find(']'):
print('出版:' + folderName.split('][')[2].split(']')[0])
print('册数:' + folderName.split('][')[2].split(']')[1])
outputFile.append('出版:' + folderName.split('][')[2].split(']')[0] + '\n')
outputFile.append('册数:' + folderName.split('][')[2].split(']')[1] + '\n')
elif folderName[-1] == ']':
print('扫者:' + folderName.split('][')[2][:-1])
outputFile.append('扫者:' + folderName.split('][')[2][:-1] + '\n')
# 文件夹格式[书名][作者][出版][扫者]Vol01
if letterCount == 8:
folderName = folderName
print('书名:' + folderName.split('][')[0][1:])
print('作者:' + folderName.split('][')[1])
print('出版:' + folderName.split('][')[2])
outputFile.append('书名:' + folderName.split('][')[0][1:] + '\n')
outputFile.append('作者:' + folderName.split('][')[1] + '\n')
outputFile.append('出版:' + folderName.split('][')[2] + '\n')
if folderName[-1] != ']' and folderName.split('][')[3].find(']'):
print('扫者:' + folderName.split('][')[3].split(']')[0])
print('册数:' + folderName.split('][')[3].split(']')[1])
outputFile.append('扫者:' + folderName.split('][')[3].split(']')[0] + '\n')
outputFile.append('册数:' + folderName.split('][')[3].split(']')[1] + '\n')
elif folderName[-1] == ']':
print('扫者:' + folderName.split('][')[3][:-1])
outputFile.append('扫者:' + folderName.split('][')[3][:-1] + '\n')
for key in sorted(numberOfFileType):
print('类型:' + key[1:] + ',数量: ' + str(numberOfFileType[key]))
outputFile.append('类型:' + key[1:] + ',数量: ' + str(numberOfFileType[key]) + '\n')
for key in fileHeight:
print('高度:' + str(key) + ', 数量: ' + str(fileHeight[key]))
outputFile.append('高度:' + str(key) + ', 数量: ' + str(fileHeight[key]) + '\n')
for key in fileWidth:
print('宽度:' + str(key) + ', 数量: ' + str(fileWidth[key]))
outputFile.append('宽度:' + str(key) + ', 数量: ' + str(fileWidth[key]) + '\n')
for key in fileWidthAndHeight:
print('宽度和高度:' + str(key) + ', 数量: ' + str(fileWidthAndHeight[key]))
outputFile.append('宽度和高度:' + str(key) + ', 数量: ' + str(fileWidthAndHeight[key]) + '\n')
print('文件数量:' + str(allFileCount))
print('文件夹大小:' + formatFileSize(allFileSize))
print('文件夹创建时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_ctime).strftime('%Y-%m-%d %H:%M:%S'))
print('文件夹修改时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S'))
outputFile.append('文件数量:' + str(allFileCount)+ '\n')
outputFile.append('文件夹大小:' + formatFileSize(allFileSize) + '\n')
outputFile.append('文件夹创建时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_ctime).strftime('%Y-%m-%d %H:%M:%S') + '\n')
outputFile.append('文件夹修改时间:' + datetime.datetime.fromtimestamp(Path(aPath).stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S') + '\n')
writeFile(Path(aPath).parent.joinpath(Path(aPath).name + '.txt'), outputFile)
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass