-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_delete3.py
More file actions
executable file
·169 lines (147 loc) · 4.81 KB
/
test_delete3.py
File metadata and controls
executable file
·169 lines (147 loc) · 4.81 KB
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
#!/usr/bin/env python3
import sys
import os
#import imageio
#image io got updated and they building next to next v2 and v3 brances.
#and there are problems with python3.6 <> python3.7 versions
if sys.version_info < (3, 7):
import imageio
else:
import imageio.v2 as imageio
import numpy as np
from PIL import Image
#print("Files and directories in '", path, "' :")
# print the list
#print(dir_list)
#ask for arguments for directory, ifn't given any , exit
def main():
"""Start main program."""
path = sys.argv[-1]
#show argv data and lenght
#print(sys.argv, len(sys.argv))
#kill ifnot arguments.
if len(sys.argv)<2:
os._exit(0)
# path = "./test_files/"
dir_list = os.listdir(path)
#sort file list, as dir_list cant be a mess order
dir_list.sort()
for filename in os.listdir(path):
#print(path)
if filename.endswith(".jpg"):
#print(filename)
#print(path)
pathfilename = path + filename
#print(pathfilename)
#print(pathfilename)
try:
img = Image.open(pathfilename)
img.verify() #I perform also verify, don't know if he sees other types o defects
img.close() #reload is necessary in my case
#img = Image.open(pathfilename)
#img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
#img.close()
except:
#manage excetions here
#dont quit, because we goind dir of files,
print("Couldtn't open image file ", pathfilename , " on img ")
#sys.exit(0)
#print(pathfilename)
f = imageio.imread(pathfilename, mode='L')
#Imageio Verify is broken and its not coming back
#print(pathfilename)
#try:
#print(pathfilename)
#imageio.verify(f)
# do stuff
#except IOError:
# filename not an image file
#print("File is not image, IO error")
#sys.exit(0)
def img_estim(img, thrshld):
is_light = np.mean(img) > thrshld
#print(np.mean(img))
#return 'light' if is_light else os.remove(filename)
if is_light == 0:
#print("Delete this file")
os.remove(pathfilename)
img_estim(f, 40)
#print(img_estim(f, 40))
#print
def test_data_dir():
path = "./test_files/"
dir_list = sorted(os.listdir(path))
data_out = 6
data_len=len(dir_list)
print(data_out - data_len)
assert data_out - data_len == 0
print(dir_list[0])
print(dir_list[1])
print(dir_list[2])
print(dir_list[3])
print(dir_list[4])
print(dir_list[5])
assert dir_list[4] == "Test_clouds.jpg"
def test_imgislight_TRUE():
path = "./test_files/"
dir_list = sorted(os.listdir(path))
#imagetreshold = 141.1277
filename = path + dir_list[4]
if sys.version_info < (3, 7):
f = imageio.imread(filename, pilmode='L')
else:
f = imageio.imread(filename, mode='L')
#Fix for older python f = imageio.imread(filename, mode='L')
is_light = np.mean(f) > 40
print(is_light)
assert is_light == True
def test_imagetresholdvalue_TRUE():
path = "./test_files/"
dir_list = sorted(os.listdir(path))
imagetresholdbig = 141.13
imagetresholdsmall = 141.11
filename = path + dir_list[4]
if sys.version_info < (3, 7):
f = imageio.imread(filename, pilmode='L')
else:
f = imageio.imread(filename, mode='L')
#Fix for older python f = imageio.imread(filename, mode='L')
meanvalue1 = np.mean(f)
meanvalue = round(meanvalue1,5)
assert imagetresholdbig >= meanvalue >= imagetresholdsmall
def test_imgislight_FALSE():
path = "./test_files/"
dir_list = sorted(os.listdir(path))
#imagetreshold = 141.1277
filename = path + dir_list[0]
if sys.version_info < (3, 7):
f = imageio.imread(filename, pilmode='L')
else:
f = imageio.imread(filename, mode='L')
#Fix for older python f = imageio.imread(filename, mode='L')
is_light = np.mean(f) > 40
print(is_light)
assert is_light != False
def test_imagetresholdvalue_FALSE():
path = "./test_files/"
dir_list = sorted(os.listdir(path))
imagetresholdbig = 39.99
imagetresholdsmall = 1.00
filename = path + dir_list[0]
if sys.version_info < (3, 7):
f = imageio.imread(filename, pilmode='L')
else:
f = imageio.imread(filename, mode='L')
#Fix for older python f = imageio.imread(filename, mode='L')
meanvalue1 = np.mean(f)
meanvalue = round(meanvalue1,5)
assert not imagetresholdbig >= meanvalue >= imagetresholdsmall
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)