-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotlight_extractor.py
145 lines (100 loc) · 4.99 KB
/
spotlight_extractor.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
## Spotlight Extractor: Automaticlly save Win 10 Spotlight Wallpapers
## 自动转存win10锁屏壁纸
class spotlightExtractor:
def __init__(self, spotlight_path, new_path, check_duplicates=True):
''' spotlight_path: location of the spotlight images.
new_path: new saving location for the spotlight images.
check_duplicates: if True, check duplicates before saving.'''
self.spotlight_path = spotlight_path
self.new_path = new_path
self.check_duplicates = check_duplicates
self.run_extractor()
def obtain_key_px(self, img_data):
'''Obtian pixel data that can distinguishe an image.'''
first, last = img_data[0], img_data[-1]
first, last = first[len(first)//5*4:], last[:len(last)//5]
return np.concatenate((first, last)) # img_data[len(img_data)//2]
def get_px_dataset(self):
'''Get key pixels data of images that are already saved in `new_path`.
Return a list containing key pixels of every exising image.'''
if 'img_ref.npz' not in os.listdir(self.new_path):
print('>> Creating image references...')
px_dataset = []
for folder in ['desktop/', 'phone/', 'ignore/']:
for img in os.listdir(self.new_path+folder):
pixel_data = self.obtain_key_px(plt.imread(self.new_path+folder+img))
px_dataset.append(pixel_data)
with open(self.new_path+'img_ref.npz', 'wb') as f:
np.savez(f, *px_dataset)
else:
with open(self.new_path+'img_ref.npz','rb') as f:
px_dataset = np.load(f)
px_dataset = [px_dataset[key] for key in px_dataset]
return px_dataset
def move_img_file(self, img, folder):
''' Move an image file to a new path.
img: the name of image file to be relocated.
folder: name of the saving folder in `new_path`.'''
os.rename(img, self.new_path+folder+'/'+img)
print('>> .\\'+folder+'\\'+img)
def find_new_images(self):
'''Check if there are new pictures in the spotlight folder
since the last time the program was run.'''
try:
push_date = datetime.fromtimestamp(os.path.getmtime(self.spotlight_path)).date()
parse_date = datetime.fromtimestamp(os.path.getmtime(self.new_path+'img_ref.npz')).date()
return (push_date != parse_date)
except:
return True
def run_extractor(self):
'''Main Function.'''
if not self.find_new_images():
return print('>> No new pics...')
for folder in ['desktop/', 'phone/', 'ignore/']:
if not os.path.exists(self.new_path+folder):
os.makedirs(self.new_path+folder)
os.chdir(self.spotlight_path)
start_saving = False
if self.check_duplicates:
print('>> Checking duplicates...')
px_dataset = self.get_px_dataset()
for num, img in enumerate(os.listdir()):
dup = False
img_data = plt.imread(img)
if self.check_duplicates:
new_key_px = self.obtain_key_px(img_data)
for data in px_dataset:
if np.array_equal(data, new_key_px):
# if the two arrays have the same shape and elements
dup = True
break
if not dup:
# if not a duplicate image or if not checking duplicates
if self.check_duplicates:
# append the key pixels of the new image for later dup checks
px_dataset.append(new_key_px)
if not start_saving:
print('>> Saving...')
start_saving = True
# copy the image and rename it using current date and its number
img_ = date.today().strftime('%Y%m%d')+'_'+str(num)+'.jpg'
copyfile(img, img_)
try:
if img_data.shape[0] < img_data.shape[1]:
# horizontal image, normally with a shape of (1080, 1920, 3)
self.move_img_file(img_, 'desktop')
else: # vertical image
self.move_img_file(img_, 'phone')
except:
print(f'Error while moving the image file {img_}...')
os.remove(img_)
if not start_saving:
print('>> No new pics...')
elif self.check_duplicates:
with open(self.new_path+'img_ref.npz', 'wb') as f:
np.savez(f, *px_dataset)
print('>> Image reference file updated.')
if __name__ == '__main__':
new_path = r'/new_path/'
spotlight_path = r'/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/'
spotlightExtractor(spotlight_path, new_path, check_duplicates=True)