-
Notifications
You must be signed in to change notification settings - Fork 0
/
photocleaner.py
277 lines (219 loc) · 8.88 KB
/
photocleaner.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import argparse
from itertools import groupby
import os
import shutil
import stat
import sys
import time
from PIL import Image
import psutil
class PhotoParser(object):
def __init__(self, photo_info, paths):
self.data_tree = {}
self.photo_info = photo_info
self.paths = paths
self.photos_by_histogram = self.__sort_by_histogram()
self.photos_to_process = self.__get_photos_to_process()
def __sort_by_histogram(self):
print('Grouping images by histogram...')
histograms = list({(p.get_histogram(), p) for p in self.photo_info})
histograms.sort(key=lambda k: k[0])
result = {}
for key, values in groupby(histograms, key=lambda x: x[0]):
result[key] = list(v[1] for v in values)
return result
def __get_photos_to_process(self):
photos_to_process = []
same_photos = 0
print('Searching for identical photos...')
for k, v in list(self.photos_by_histogram.items()):
photos_to_process.append(v[0])
if len(v) > 1:
print('Identical photos found:')
for img in v:
print(' ' + img.get_file_name())
same_photos = same_photos + 1
if same_photos == 0:
print("There are no identical photos, there's nothing to do.")
sys.exit(-1)
return photos_to_process
def process(self):
try:
# Order photos by creation date
print('%d photos to process...' % len(self.photos_to_process))
self.photos_to_process.sort(key=lambda x: x.get_year())
years = list(set([p.get_year() for p in self.photos_to_process]))
for y in years:
self.data_tree[y] = {}
for y in years:
for p in self.photos_to_process:
print('Processing %s - Date: %s' % (p.get_file_name(), p.get_file_date()))
months = list(set([p.get_month() for p in self.photos_to_process if y == p.get_year()]))
for m in months:
self.data_tree[y][m] = []
for p in self.photos_to_process:
self.data_tree[p.get_year()][p.get_month()].append(p)
# Data tree is completed.
self.__create_directory_tree()
self.__copy_files()
except Exception as e:
print('Error processing data:')
print(repr(e))
sys.exit(-1)
def __create_directory_tree(self):
try:
for year, v in list(self.data_tree.items()):
for month, data in list(v.items()):
create_directory(''.join([self.paths[1], os.sep, year, os.sep, month]))
except Exception as e:
print(e)
def __copy_files(self):
try:
total = len(self.photos_to_process)
index = 0
for year, v in list(self.data_tree.items()):
for month, data in list(v.items()):
for d in data:
path = d.get_file_name()
filename = path[path.rfind(os.sep) + 1:]
output = ''.join([self.paths[1], os.sep, year, os.sep, month, os.sep, str(index).zfill(6), '_', filename])
print(output)
shutil.copy2(path, output)
index += 1
print('[%d%%] - Copying %s to %s' % (((index * 100) / total), filename, output))
print('Done!')
except Exception as e:
print('Error copying files')
print(repr(e))
sys.exit(-1)
class PhotoInfo(object):
def __init__(self, file_info):
self.file_info = file_info
self.histogram = None
def set_histogram(self, histogram):
self.histogram = histogram
def get_histogram(self):
return self.histogram
def get_file_size(self):
return self.file_info['fsize']
def get_file_name(self):
return self.file_info['fname']
def get_file_date(self):
return self.file_info['f_mt']
def get_year(self):
return self.file_info['f_mt'][:4]
def get_month(self):
return self.file_info['f_mt'][5:7]
class PhotoCleaner(object):
def __init__(self, paths):
self.photo_info = {}
self.file_list = []
self.input_path = paths[0]
self.output_path = paths[1]
self.images_found = 0
self.__scan_path_for_images()
self.photo_info = self.__create_photo_info_objects()
self.__analyze_histogram()
def __scan_path_for_images(self):
self.file_list = [os.path.join(x[0], y) for x in os.walk(self.input_path) for y in x[2]]
self.img_list = [file for file in self.file_list if file[-4:].lower() in ('jpeg', '.jpg', '.png')]
self.images_found = len(self.img_list)
if self.images_found == 0:
print('There are no images in the specified path')
sys.exit(-1)
else:
print('Found %d images' % self.images_found)
def __create_photo_info_objects(self):
needed_free_space = 0
print('Calculating free space...')
photo_info = []
for f in self.img_list:
file_stats = os.stat(f)
file_info = {
'fname': f,
'fsize': file_stats[stat.ST_SIZE],
'f_mt': time.strftime("%Y-%m-%d %I:%M:%S%p", time.localtime(file_stats[stat.ST_MTIME]))
}
needed_free_space += file_stats[stat.ST_SIZE]
photo_info.append(PhotoInfo(file_info))
free_space = self.__get_free_space()
if free_space - needed_free_space < 0:
print('%ld free bytes on disk are needed to work, only %ld bytes are available' % (needed_free_space, free_space))
sys.exit(-1)
else:
print('%ld bytes of free disk space are available, %ld bytes needed' % (free_space, needed_free_space))
return photo_info
@staticmethod
def __get_free_space():
return psutil.disk_usage(".").free
def __analyze_histogram(self):
print('Analyzing histograms...')
try:
# index = 0
# total = len(self.photo_info)
for i in self.photo_info:
try:
image = Image.open(i.get_file_name())
histogram = hash(str(image.histogram()))
i.set_histogram(histogram)
# index += 1
# print('Completed %d%% - %s' % ((100 * index) / total, i.get_file_name()))
except Exception:
print('Delete: %s' % i.get_file_name())
self.photo_info.remove(i)
# i.set_histogram(0)
# print(repr(ex))
except Exception as e:
print('Error analyzing histograms')
print(repr(e))
sys.exit(-1)
def get_photo_info(self):
return self.photo_info
def main():
paths = get_paths()
prepare_paths(paths)
pc = PhotoCleaner(paths)
photo_info = pc.get_photo_info()
ps = PhotoParser(photo_info, paths)
# ps.process()
def prepare_paths(paths):
# Input path
if not os.path.isdir(paths[0]):
print('Input a valid input directory')
sys.exit(-1)
# Output path
if not os.path.isdir(paths[1]):
# Trying to create the output_path
create_directory(paths[1])
else:
return
# Directory exists, delete contents?
result = input('The directory ' + paths[1] + ' exists, do you want to delete its contents? (y/N)')
if result.lower() == 'y':
sure_result = input('Are you totally sure? (y/N)')
if sure_result.lower() == 'y':
# Delete contents of directory
delete_output_path(paths[1])
return
print('Aborting...')
sys.exit(-1)
def get_paths():
parser = argparse.ArgumentParser('photocleaner')
parser.add_argument('input_path', help='input_path points the path for image searching')
parser.add_argument('output_path', help='output_path where images will be saved when processed')
args = parser.parse_args()
return args.input_path, args.output_path
def create_directory(path):
try:
os.makedirs(path)
except Exception as e:
print('Unable to make directory ' + path)
def delete_output_path(output_path):
try:
os.rmdir(output_path)
create_directory(output_path)
except Exception as e:
print('Unable to delete directory ' + output_path)
sys.exit(-1)
if __name__ == '__main__':
main()