-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete5_multipool.py
More file actions
executable file
·84 lines (72 loc) · 2.31 KB
/
delete5_multipool.py
File metadata and controls
executable file
·84 lines (72 loc) · 2.31 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
import multiprocessing
import sys
import os
import numpy as np
from PIL import Image
if sys.version_info < (3, 7):
import imageio
else:
import imageio.v2 as imageio
def process(pathfilename):
#pass # do stuff to a file
f = imageio.imread(pathfilename, mode='L')
is_light = np.mean(f) > 40
#Print lightness value
#print(np.mean(f))
#alternative way to do, when calling returns
#return 'light' if is_light else os.remove(filename)
if is_light == 0:
#print if file will be deleted
#print("test DELETE ", pathfilename)
#print('parent process:', os.getppid())
#print('process id:', os.getpid())
os.remove(pathfilename)
# if is_light == 1:
#print if file is keep
# print("test KEEP ", pathfilename)
# print('parent process:', os.getppid())
# print('process id:', os.getpid())
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)
if os.path.exists(path):
dir_list = os.listdir(path)
#sort file list, as dir_list cant be a mess order
dir_list.sort()
else:
print("Couldn't open path: ", path)
os._exit(0)
p = multiprocessing.Pool()
for filename in os.listdir(path):
if filename.endswith(".jpg"):
pathfilename = path + filename
#print(pathfilename)
try:
#Test files that can they be opended
img = Image.open(pathfilename)
#Perform also verify, don't know if he sees other types o defects
img.verify()
#close file
img.close()
except:
#dont quit. just display that file couldn't been opended.
print("Couldn't open image file: ", pathfilename)
# launch a process for each file (ish), using async ending.
# The result will be approximately one process per CPU core available.
p.apply_async(process, [pathfilename])
p.close()
p.join() # Wait for all child processes to close.
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)