forked from wangandi520/andyspythonscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path获取图片宽度和对应文件数量(拖拽,递归,多个图片文件或文件夹).py
90 lines (82 loc) · 4 KB
/
获取图片宽度和对应文件数量(拖拽,递归,多个图片文件或文件夹).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
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
# pip install pillow
from PIL import Image
from pathlib import Path
import sys
def main(inputPath):
# 显示每张图片的宽度 = True, 不显示 = False
showEachFile = True
# 显示完整路径 = True,只显示文件名 = False
showAllPath = False
# 文件格式
fileType = ['.jpg', '.png']
fileWidth = {}
filePathStorage = []
del inputPath[0]
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('**/*'):
if Path.is_file(file) and (file.suffix.lower() in fileType):
tmpImg = Image.open(file)
if (tmpImg.size[0] not in fileWidth):
if (showEachFile and showAllPath):
print(str(Path(file)) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(file)), str(tmpImg.size[0])])
if (showEachFile and not showAllPath):
print(str(Path(file.name)) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(file.name)), str(tmpImg.size[0])])
fileWidth[tmpImg.size[0]] = 1
elif (tmpImg.size[0] in fileWidth):
if (showEachFile and showAllPath):
print(str(Path(file)) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(file)), str(tmpImg.size[0])])
if (showEachFile and not showAllPath):
print(str(Path(file.name)) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(file.name)), str(tmpImg.size[0])])
fileWidth[tmpImg.size[0]] = fileWidth[tmpImg.size[0]] + 1
if Path.is_file(Path(aPath)) and (Path(aPath).suffix.lower() in fileType):
tmpImg = Image.open(aPath)
if (tmpImg.size[0] not in fileWidth):
if (showEachFile and showAllPath):
print(str(Path(aPath)) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(aPath)), str(tmpImg.size[0])])
if (showEachFile and not showAllPath):
print(str(Path(aPath).name) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(aPath).name), str(tmpImg.size[0])])
fileWidth[tmpImg.size[0]] = 1
elif (tmpImg.size[0] in fileWidth):
if (showEachFile and showAllPath):
print(str(Path(aPath)) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(aPath)), str(tmpImg.size[0])])
if (showEachFile and not showAllPath):
print(str(Path(aPath).name) + ' ' + str(tmpImg.size[0]))
filePathStorage.append([str(Path(aPath).name), str(tmpImg.size[0])])
fileWidth[tmpImg.size[0]] = fileWidth[tmpImg.size[0]] + 1
print()
for key in fileWidth:
print('图片宽度: ' + str(key) + ', 文件数量: ' + str(fileWidth[key]))
print()
print('输入数字查看对应宽度的全部文件:')
for index, key in enumerate(fileWidth, start=1):
print(str(index) + ':' + str(key))
print()
getInput = input('输入回车退出: ')
print()
while (getInput != ''):
if int(getInput) <= len(fileWidth):
getInput = int(getInput) - 1
tmpHeight = list(fileWidth.keys())[getInput]
print('宽度' + str(tmpHeight) + ':')
for eachFile in filePathStorage:
if eachFile[1] == str(tmpHeight):
print(eachFile[0])
print()
getInput = input('输入回车退出或右上角关闭: ')
print()
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass