-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotlight.py
99 lines (86 loc) · 3.05 KB
/
spotlight.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
import os
import shutil
from PIL import Image
import imagehash
def rename(path, newpath):
try:
os.rename(path, newpath)
print(path, newpath, sep=" ===> ")
except FileExistsError:
os.remove(path)
print("Duplicate file:", newpath)
except PermissionError:
print("Permission denied:", path)
pass
def copy(path, newpath):
shutil.copy2(path, newpath)
print(path, newpath, sep=" ===> ")
def fix_file_locations():
print("Fixing file locations...")
for root, dirs, files in os.walk(orig):
for name in files:
if name.endswith('.jpg'):
path = os.path.join(root, name)
im = Image.open(path)
width, height = im.size
im.close()
if width > height:
newpath = os.path.join(wide, name)
rename(path, newpath)
elif height > width:
newpath = os.path.join(tall, name)
rename(path, newpath)
for root, dirs, files in os.walk(dest_dir):
for name in files:
if name.endswith('.jpg.jpg'):
path = os.path.join(root, name)
newpath = os.path.join(root, name[:-4])
rename(path, newpath)
def fix_hash_for_all():
print("Fixing hashes...")
thresh = 1920*1080 - 32*32
for root, dirs, files in os.walk(dest_dir):
for name in files:
if name.endswith('.jpg') and len(name) == 14:
path = os.path.join(root, name)
ext = name[-4:]
im = Image.open(path)
hsh = imagehash.phash(im)
width, height = im.size
im.close()
newpath = os.path.join(root, str(hsh) + ext)
if width*height > thresh:
rename(path, newpath)
fix_file_locations()
def collect_files():
print("Collecting files...")
thresh = 1920*1080 - 32*32
for dir in (source_dir, dest_dir, orig, tall, wide):
if not os.path.isdir(dir):
os.mkdir(dir)
for root, dirs, files in os.walk(source_dir):
for name in files:
path = os.path.join(root, name)
try:
im = Image.open(path)
width, height = im.size
except:
continue
if width*height > thresh:
hsh = imagehash.phash(im)
im.close()
if width > height:
newpath = os.path.join(wide, str(hsh)+'.jpg')
copy(path, newpath)
elif height > width:
newpath = os.path.join(tall, str(hsh)+'.jpg')
copy(path, newpath)
if __name__ == "__main__":
source_dir = os.environ['LocalAppData'] + r"\Packages\Microsoft.Windows." \
+ r"ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
dest_dir = os.getcwd()
orig = dest_dir + r"\Originals"
tall = dest_dir + r"\Tall"
wide = dest_dir + r"\Wide"
fix_hash_for_all()
collect_files()