-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
167 lines (149 loc) · 5.92 KB
/
main.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
import os
import PIL.Image
from PIL import Image
import csv
def get_path(dirs_to_check):
"""Checks path input"""
input_path = input(wh + 'Specify path to folder/s containing images: ')
try:
os.path.exists(input_path)
except NotADirectoryError:
print('Path is not valid, quitting')
quit()
return input_path
def get_dirs(input_path, dirs_to_check):
"""Lists dirs from input"""
image_path = input_path
for root, dirs, files in os.walk(image_path):
dirs_to_check.append(root)
for i in dirs:
dir_path = root + i
dirs_to_check.append(dir_path)
def get_images(dirs_to_check):
"""Lists images to a set"""
images_set = set()
for i in dirs_to_check:
for root, dirs, files in os.walk(i):
for f_name in files:
if (not f_name.lower().endswith('.jp2')
and not f_name.lower().endswith('.jpg')
and not f_name.lower().endswith('.jpeg')
and not f_name.lower().endswith('.tif')
and not f_name.lower().endswith('.tiff')
and not f_name.lower().endswith('.png')
and not f_name.lower().endswith('.jxl')):
continue
else:
file = os.path.normpath(root) + '/' + f_name
images_set.add(file)
if len(images_set) == 0:
print('None image has been found, quitting.')
quit()
return images_set
def is_corrupted(image, decomp_bomb):
"""Checks images"""
try:
with Image.open(image) as img:
img.verify()
return False
except PIL.Image.DecompressionBombError:
decomp_bomb.append(image)
return False
except PIL.Image.DecompressionBombWarning:
decomp_bomb.append(image)
return False
except:
return True
def corrupted(images_set):
"""Appends corrupted images to the set"""
corrupted = []
for image in images_set:
if is_corrupted(image, decomp_bomb):
corrupted.append(image)
return corrupted
def check_nums(images_set, corrupted, decomp_bomb, path, dirs_to_check):
"""Returns numbers of corrupted images grouped by formats"""
jpg = []
png = []
tif = []
jp2 = []
jxl = []
for i in corrupted:
if '.jp2' in str(i):
jp2.append(i)
elif '.jxl' in str(i):
jxl.append(i)
elif '.png' in str(i):
png.append(i)
elif '.tif' in str(i) or '.tiff' in str(i):
tif.append(i)
elif '.jpeg' in str(i) or '.jpg' in str(i):
jpg.append(i)
else:
pass
if len(corrupted) == 0:
print('Result: ' + str(len(images_set) - len(decomp_bomb)) + ' images inside ' + str(dirs_to_check[0]) + ' were'
' checked succesfully.'
'\n Images with Decompression Error/Warnings found: ' + (str(len(decomp_bomb))))
else:
print('Result:\nImages with Errors found in total: ' + str(len(corrupted) - len(jxl)))
print('From which: \nCorrupted JPEGs: ' + (str(len(jpg))), '\nCorrupted JPEG2000s: ' + (str(len(jp2))),
'\nCorrupted PNGs: ' + (str(len(png))), '\nCorrupted TIFFs: ' +
(str(len(tif))), '\nImages with Decompression Error/Warnings: ' + (str(len(decomp_bomb))),
'\nJXLs (Note: JXLs can´t be checked using PIL module): ' + (str(len(jxl))))
csv_file = (path + '/reports/corrupted_list.csv')
if len(corrupted) > 0 or len(decomp_bomb) > 0:
print(rd + "Result with details can be found inside /reports/")
else:
pass
with open(csv_file, 'w', encoding='utf8') as f:
writer = csv.writer(f)
f.write('Images checked by Python Pillow Module - Corrupted_Images_Script by NACR' +
'\n-------------------------------------------------------------------------\n' +
'\nNumb. of images checked in total: ' + str(len(images_set))
+ '\n\n-------------------------------------------------------------------------\n'
+ 'Corrupted:\n')
for i in corrupted:
f.write(str(i) + '\n')
f.write('\n-------------------------------------------------------------------------\n' +
'Images which can not be checked due to DecompressionBombError/Warning:')
for i in decomp_bomb:
f.write(str(i) + '\n')
f.close()
return jpg, png, tif, jp2, jxl
if __name__ == '__main__':
logo_small = """
NNN NN AAAAA CCCCC RRRR
NN NN NN AA AA CCC RR RR
NN NN NN AAAAAAA CCC RRRRR
NN NNN AA AA CCCCC RR RR
"""
bl = '\033[1;34;40m'
cy = '\033[96m'
wh = '\33[97m'
rd = '\33[91m'
print(bl + logo_small)
user = (os.getlogin())
user = user.title()
separator = '='
padding = str('\/' * 10)
welcome = padding + ' Dear user "' + str(user) + '", welcome to Corrupted_Images by NAČR. ' + padding
print(cy + welcome)
print((len(welcome) * separator))
path = (os.path.dirname(__file__))
try:
os.makedirs(path + '/reports/')
except FileExistsError:
pass
dirs_to_check = []
input_path = get_path(dirs_to_check)
get_dirs(input_path, dirs_to_check)
images_set = get_images(dirs_to_check)
unidentified = []
decomp_bomb = []
if len(images_set) == 1:
print('One image has been found.')
else:
print('Images have been found in total: ' + str(len(images_set)))
corrupted = corrupted(images_set)
jpg, png, tif, jp2, jxl = check_nums(images_set, corrupted, decomp_bomb, path, dirs_to_check)