-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_compressor.py
More file actions
317 lines (249 loc) · 9.87 KB
/
image_compressor.py
File metadata and controls
317 lines (249 loc) · 9.87 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
import datetime
import os
import inspect
import time
import shutil
import platform
from PIL import Image, ImageOps
from multiprocessing import Process
def chunk_list(lst, n):
"""yields n evenly spaced arrays."""
for i in range(0, n):
yield lst[i::n]
def compressing_loop(compressed_dir, script_dir, chunked_list):
for jpg in chunked_list:
# path to work with
old_path = os.path.join(script_dir, jpg)
with Image.open(fp=old_path) as image:
date = jpg_date(image, jpg)
year = date[:4]
# name of new dir and path from specific year
new_dir = os.path.join(compressed_dir, str('IMG_'+year))
new_path = str(os.path.join(new_dir, jpg)).replace(
'.jpg', '_compressed.jpg')
try:
# new dir available?
if os.path.exists(new_dir) is False:
os.mkdir(new_dir)
# image already available -> skip
if os.path.exists(new_path):
continue
# happened once, not sure why
except FileExistsError:
continue
# necessary to keep orientation
try:
image = ImageOps.exif_transpose(image)
except AttributeError:
pass
# resize
# newsize = image.width//2, image.height//2
# image = image.resize(newsize, Image.ANTIALIAS)
# save with new name, new size and lower quality
try:
# with available exif data
image.save(new_path, quality=50, subsampling=0, exif=image.info["exif"])
image.close()
except KeyError:
# without available exif data
image.save(new_path, quality=50, subsampling=0)
image.close()
try:
set_modified_date(new_path, date)
except Exception:
pass
def copying_loop(compressed_dir, script_dir, copy_targets):
for copy_target in copy_targets:
# files and directory infos
old_path = os.path.join(script_dir, copy_target)
jpg_dt = jpg_date(old_path, copy_target)
jpg_year = jpg_dt[:4]
new_dir = os.path.join(compressed_dir, str('IMG_'+jpg_year))
new_path = str(os.path.join(new_dir, copy_target)
).replace('.jpg', '_copy.jpg')
try:
# new dir available?
if os.path.exists(new_dir) is False:
os.mkdir(new_dir)
# image already copied?
if os.path.exists(new_path):
pass
# happened once, not sure why... ignore
except FileExistsError:
continue
# copy file
shutil.copyfile(src=old_path, dst=new_path)
print(f'Copied without compressing: {copy_target}')
#set modification date
try:
set_modified_date(new_path, jpg_dt)
except Exception:
pass
def jpg_targets_lists(script_dir):
"""returns lists of valid .jpg file names including extension for a)compressing and b)copying"""
all_filenames = os.listdir(script_dir)
target_jpg_list = []
copy_list = []
for filename in all_filenames:
if (filename.split('.')[-1] == 'jpg' and # extension is '.jpg'
filename[0] != '.'): # not hidden
target_jpg_list.append(filename)
# contains '.jpg', not hidden
elif ('.jpg' in filename and filename[0] != '.'):
copy_list.append(filename)
return target_jpg_list, copy_list
def jpg_date(img, filename):
"""takes eighter PIL Image-Object or filepath for "img" and a filename. returns date as string in yyyy:mm:dd, see fallback options"""
if isinstance(img, str):
# it's not a PIL-object!
date = date_from_name_pattern(filename) # risky, but effective
else:
# It's a PIL-object!
date = date_from_PIL_exif(img)
if date is None:
date = date_from_name_pattern(filename)
img = img.filename # (img is now filepath)
if date is None:
# ...not found, fallback
date = date_from_os(img)
if date is None:
date = "0000:01:01"
return date
def date_from_os(filepath):
'''takes filepath to file'''
date = None
if platform.system() == 'Windows':
c_time = time.ctime(os.path.getctime(filepath))
c_date_obj = datetime.datetime.strptime(c_time, "%a %b %d %H:%M:%S %Y") #datetime obj
date = c_date_obj.strftime("%Y:%m:%d") # formated
else:
try:
stat = os.stat(filepath)
c_time = int(time.ctime(stat.st_birthtime))
c_date_obj = datetime.datetime.strptime(c_time, "%a %b %d %H:%M:%S %Y") #datetime obj
date = c_date_obj.strftime("%Y:%m:%d") # formated
except Exception:
# We're probably on Linux. No easy way to get creation dates here
# or something else went wrong
# -> take YYYY
date = None
return date
def date_from_name_pattern(filename):
'''search in filename for common naming-convention...)'''
date = None
filename = str(filename).upper()
prefix_list = ['IMG_', 'IMG-', 'IMG', 'WHATSAPP_IMAGE_',
'SCREENSHOT_', 'SCREENSHOT-', 'SIGNAL-', 'PXL_']
for prefix in prefix_list:
if prefix in filename:
try:
date = str(int(filename.split(prefix)[1][:8]))
# trying to convert as double-check
date_obj = datetime.datetime.strptime(date, '%Y%m%d')
# and bring to exif format
date = date_obj.strftime("%Y:%m:%d")
except Exception:
continue
return date
def date_from_PIL_exif(img):
try:
date = str(img._getexif()[36867])
#exif date output example:
# 2018:08:25 16:38:43
return date[:10]
except Exception:
return None
def count_files_in_dirs(output_dir):
# recursive
file_amount = 0
for dirpath, dirnames, filenames in os.walk(output_dir):
for f in filenames:
fp = os.path.join(dirpath, f)
if not os.path.islink(fp):
file_amount += 1
return file_amount
def set_modified_date(filepath, date):
# set modification date of the copy to creation date.
# change creation date not yet possible os-wide
date_timestamp = datetime.datetime.strptime(date, "%Y:%m:%d").timestamp()
os.utime(filepath, (time.time(), date_timestamp))
def progress_bar(compressed_dir, total, barLength=20):
'''prints out a nice progress bar by checking number of files'''
count = True
while count is True:
current = count_files_in_dirs(compressed_dir)
percent = float(current) * 100 / total
arrow = '-' * int(percent/100 * barLength - 1) + '>'
spaces = ' ' * (barLength - len(arrow))
# print actual bar:
print('Progress: [%s%s] %d %%' % (arrow, spaces, percent), end='\r')
if percent >= 100:
count = False
print('Progress: [%s%s] %d %%' % (arrow, spaces, percent))
else:
time.sleep(1)
def main():
try:
# where am i? Path infos
scrip_path = inspect.getframeinfo(inspect.currentframe()).filename
script_dir = os.path.dirname(os.path.abspath(scrip_path))
compressed_dir = os.path.join(script_dir, 'IMG_compressed')
# find targets
compress_jpg_targets, copy_jpg_targets = jpg_targets_lists(script_dir)
jpg_compress_amout = len(compress_jpg_targets)
jpg_copy_amount = len(copy_jpg_targets)
# 0 targets ->stop
if not jpg_compress_amout and not jpg_copy_amount:
print('No files to compress in this directory')
else:
print(f'\nFound {jpg_compress_amout} valid .jpg files to compress')
print(
f'Found {jpg_copy_amount} .jpg files to copy without compressing\n')
if not copy_jpg_targets:
print()
else:
print(
'For the following copying-targets, year is taken from file creation date, wich is not always acurate')
# chunk targets for 4 processes
chunk_1, chunk_2, chunk_3, chunk_4 = chunk_list(
compress_jpg_targets, 4)
# ...foo/bar/IMG_compressed folder available?
try:
if os.path.exists(compressed_dir) is False:
os.mkdir(compressed_dir)
except FileExistsError:
pass
# copy strange .jpg files without compressing
copying_loop(compressed_dir, script_dir, copy_jpg_targets)
# go!
p1 = Process(target=compressing_loop, args=(
compressed_dir, script_dir, chunk_1))
p2 = Process(target=compressing_loop, args=(
compressed_dir, script_dir, chunk_2))
p3 = Process(target=compressing_loop, args=(
compressed_dir, script_dir, chunk_3))
p4 = Process(target=compressing_loop, args=(
compressed_dir, script_dir, chunk_4))
process_list = [p1, p2, p3, p4]
# start processes
for p in process_list:
p.start()
# verbosity while running
progress_bar(compressed_dir, (jpg_compress_amout +
jpg_copy_amount), barLength=40)
print()
# close processes
for p in process_list:
p.join()
for p in process_list:
p.close
except KeyboardInterrupt:
# close processes
for p in process_list:
p.join()
for p in process_list:
p.close
print('[ctr+c -> byebye]]')
if __name__ == '__main__':
main()