1- """ Script To Copy Spotlight(Lockscreen) Images from Windows """
1+ """ Script To Copy Spotlight(Lockscreen) Images from Windows """
22from __future__ import print_function
3- import os
4- import shutil
5- import errno
6- import hashlib
7- from PIL import Image
8-
9- def md5 (fname ):
10- """ Function to return the MD5 Digest of a file """
11-
12- hash_md5 = hashlib .md5 ()
13- with open (fname , "rb" ) as file_var :
14- for chunk in iter (lambda : file_var .read (4096 ), b"" ):
15- hash_md5 .update (chunk )
16- return hash_md5 .hexdigest ()
17-
18- def make_folder (folder_name ):
19- """Function to make the required folers"""
20- try :
21- os .makedirs (folder_name )
22- except OSError as exc :
23- if exc .errno == errno .EEXIST and os .path .isdir (folder_name ):
24- pass
25- else :
26- print ("Error! Could not create a folder" )
27- raise
28-
29- def get_spotlight_wallpapers (target_folder ):
30- """Fetches wallpapers from source folder inside AppData to the
31- newly created folders in C:\\ Users\\ ['user.name']\\ Pictures"""
32- #PATHS REQUIRED TO FETCH AND STORE WALLPAPERS
33- #Creating necessary folders
34-
35- source_folder = os .environ ['HOME' ]+ "\\ AppData\\ Local\\ Packages\\ "
36- source_folder += "Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy"
37- source_folder += "\\ LocalState\\ Assets"
38- spotlight_path_mobile = target_folder + "\\ Mobile"
39- spotlight_path_desktop = target_folder + "\\ Desktop"
40- make_folder (spotlight_path_mobile )
41- make_folder (spotlight_path_desktop )
42-
43-
44- #Fetching files from the source dir
45- for filename in os .listdir (source_folder ):
46- filename = source_folder + "\\ " + filename
47- #if size of file is less than 100 KB, ignore the file
48- if os .stat (filename ).st_size > 100000 :
49- #Check resolution and classify based upon the resolution of the images
50-
51- #name the file equal to the MD5 of the file, so that no duplicate files are to be copied
52- img_file = Image .open (filename )
53- if img_file .size [0 ] >= 1080 :
54- if img_file .size [0 ] > img_file .size [1 ]:
55- temp_path = spotlight_path_desktop + "\\ " + md5 (filename )
56- else :
57- temp_path = spotlight_path_mobile + "\\ " + md5 (filename )
58- #If file doesn't exist, copy the file to the new folders
59- if not os .path .exists (temp_path + ".png" ):
60- shutil .copy (filename , temp_path + ".png" )
61-
62- if __name__ == '__main__' :
63- PATH = raw_input ("Enter directory path:" )
64- get_spotlight_wallpapers (PATH )
65- print ("Lockscreen images have been copied to \" " + PATH + "\" " )
66-
3+ import os
4+ import shutil
5+ import errno
6+ import hashlib
7+ from PIL import Image
8+
9+ def md5 (fname ):
10+ """ Function to return the MD5 Digest of a file """
11+
12+ hash_md5 = hashlib .md5 ()
13+ with open (fname , "rb" ) as file_var :
14+ for chunk in iter (lambda : file_var .read (4096 ), b"" ):
15+ hash_md5 .update (chunk )
16+ return hash_md5 .hexdigest ()
17+
18+ def make_folder (folder_name ):
19+ """Function to make the required folers"""
20+ try :
21+ os .makedirs (folder_name )
22+ except OSError as exc :
23+ if exc .errno == errno .EEXIST and os .path .isdir (folder_name ):
24+ pass
25+ else :
26+ print ("Error! Could not create a folder" )
27+ raise
28+
29+ def get_spotlight_wallpapers (target_folder ):
30+ """Fetches wallpapers from source folder inside AppData to the
31+ newly created folders in C:\\ Users\\ ['user.name']\\ Pictures"""
32+ #PATHS REQUIRED TO FETCH AND STORE WALLPAPERS
33+ #Creating necessary folders
34+
35+ source_folder = os .environ ['HOME' ]+ "\\ AppData\\ Local\\ Packages\\ "
36+ source_folder += "Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy"
37+ source_folder += "\\ LocalState\\ Assets"
38+ spotlight_path_mobile = target_folder + "\\ Mobile"
39+ spotlight_path_desktop = target_folder + "\\ Desktop"
40+ make_folder (spotlight_path_mobile )
41+ make_folder (spotlight_path_desktop )
42+
43+
44+ #Fetching files from the source dir
45+ for filename in os .listdir (source_folder ):
46+ filename = source_folder + "\\ " + filename
47+ #if size of file is less than 100 KB, ignore the file
48+ if os .stat (filename ).st_size > 100000 :
49+ #Check resolution and classify based upon the resolution of the images
50+
51+ #name the file equal to the MD5 of the file, so that no duplicate files are to be copied
52+ img_file = Image .open (filename )
53+ if img_file .size [0 ] >= 1080 :
54+ if img_file .size [0 ] > img_file .size [1 ]:
55+ temp_path = spotlight_path_desktop + "\\ " + md5 (filename )
56+ else :
57+ temp_path = spotlight_path_mobile + "\\ " + md5 (filename )
58+ #If file doesn't exist, copy the file to the new folders
59+ if not os .path .exists (temp_path + ".png" ):
60+ shutil .copy (filename , temp_path + ".png" )
61+
62+ if __name__ == '__main__' :
63+ PATH = raw_input ("Enter directory path:" )
64+ get_spotlight_wallpapers (PATH )
65+ print ("Lockscreen images have been copied to \" " + PATH + "\" " )
66+
67+
68+
0 commit comments