自從用了 Win10,當開機的時候時不時會被驚艷到,有些推薦的背景圖片真的很好看,比如下面這種:
然後就開始在網上找方法,怎麼下載這些圖片。然後的確有方法,就是去到一個神不知鬼不覺的文件目錄:C:\Users\用戶名\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets
然後看到一堆什麼亂七八糟的文件,就是這些圖片存在的地方。
這些壁紙應該都是 Win10 從微軟服務器下載的,所以不同人的電腦壁紙不一樣
這些文件都默認沒有後綴名,然後我們把上面的文件增加 .jpg
的後綴才可以看到圖片的廬山真面目,這裡當然可以手動增加後綴名;
也可以把上訴文件復制到另一個位置,比如 E 盤的 Wallpaper 目錄下,然後用 cmd 的批量重命名工具,輸入以下命令:
ren E:\Wallpaper\* *.jpg
然後就能正確查看這些圖片了。可以=看到圖片有橫版和豎版的,分別為電腦端和手機端:
你會看到,一般每個畫面有兩張壁紙,一張為 1920 * 1080 分辨率的橫屏桌面壁紙,一張為 1080 * 1920 分辨率的豎屏手機桌面壁紙。
現在你就可以把這些圖片用作其他電腦或手機的桌面壁紙了。
使用快捷鍵 WIN + R 打開如下的運行界面:
輸入如下的值,快速打開我們的文件夾:
%localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets
把我們的文章復制出來,更改文件後綴,比如我在此處加上 .png
:
作為程序員,怎麼能自己手動一個個該文件後綴呢,不能忍受,所以我們利用 Python 這個腳本語言來進行自動化處理。
A Windows 10 PC
Python 3x
PIL Module For Python 3x
我們需要讀取當前的用戶名
# Set Environment Variablesusername = os.environ['USERNAME']
拼裝文件路徑:
# All file urls file_urls = { "wall_src": "C:\\Users\\" + username + "\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\" + "LocalState\\Assets\\", "wall_dst": os.path.dirname(os.path.abspath(__file__)) + "\\Wallpapers\\", "wall_mobile": os.path.dirname(os.path.abspath(__file__)) + "\\Wallpapers\\mobile\\", "wall_desktop": os.path.dirname(os.path.abspath(__file__)) + "\\Wallpapers\\desktop\\" }
復制出文件:
# A method to import the wallpapers from src folder(dir_src) @staticmethod def copy_wallpapers(): w = Wallpaper w.time_gap("Copying Wallpapers") # Copy All Wallpapers From Src Folder To Dest Folder for filename in os.listdir(w.file_urls["wall_src"]): shutil.copy(w.file_urls["wall_src"] + filename, w.file_urls["wall_dst"])
添加文件後綴:
# A method to Change all the Extensions @staticmethod def change_ext(): w = Wallpaper w.time_gap("Changing Extensions") # Look into all the files in the executing folder and change extension for filename in os.listdir(w.file_urls["wall_dst"]): base_file, ext = os.path.splitext(filename) if ext == "": if not os.path.isdir(w.file_urls["wall_dst"] + filename): os.rename(w.file_urls["wall_dst"] + filename, w.file_urls["wall_dst"] + filename + ".jpg")
完整代碼如下:
import osimport shutilimport timefrom PIL import Imageclass Wallpaper: # Set Environment Variables username = os.environ['USERNAME'] # All file urls file_urls = { "wall_src": "C:\\Users\\" + username + "\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\" + "LocalState\\Assets\\", "wall_dst": os.path.dirname(os.path.abspath(__file__)) + "\\Wallpapers\\", "wall_mobile": os.path.dirname(os.path.abspath(__file__)) + "\\Wallpapers\\mobile\\", "wall_desktop": os.path.dirname(os.path.abspath(__file__)) + "\\Wallpapers\\desktop\\" } msg = ''' DDDDD OOOOO NN N EEEEEEE D D O O N N N E D D O O N N N E D D O O N N N EEEE D D O O N N N E D D O O N N N E DDDDD OOOOO N NN EEEEEEE ''' # A method to showcase time effect @staticmethod def time_gap(string): print(string, end='') time.sleep(1) print(".", end='') time.sleep(1) print(".") # A method to import the wallpapers from src folder(dir_src) @staticmethod def copy_wallpapers(): w = Wallpaper w.time_gap("Copying Wallpapers") # Copy All Wallpapers From Src Folder To Dest Folder for filename in os.listdir(w.file_urls["wall_src"]): shutil.copy(w.file_urls["wall_src"] + filename, w.file_urls["wall_dst"]) # A method to Change all the Extensions @staticmethod def change_ext(): w = Wallpaper w.time_gap("Changing Extensions") # Look into all the files in the executing folder and change extension for filename in os.listdir(w.file_urls["wall_dst"]): base_file, ext = os.path.splitext(filename) if ext == "": if not os.path.isdir(w.file_urls["wall_dst"] + filename): os.rename(w.file_urls["wall_dst"] + filename, w.file_urls["wall_dst"] + filename + ".jpg") # Remove all files Not having Wallpaper Resolution @staticmethod def extract_wall(): w = Wallpaper w.time_gap("Extracting Wallpapers") for filename in os.listdir(w.file_urls["wall_dst"]): base_file, ext = os.path.splitext(filename) if ext == ".jpg": try: im = Image.open(w.file_urls["wall_dst"] + filename) except IOError: print("This isn't a picture.", filename) if list(im.size)[0] != 1920 and list(im.size)[0] != 1080: im.close() os.remove(w.file_urls["wall_dst"] + filename) else: im.close() # Arrange the wallpapers into the corresponding folders @staticmethod def arr_desk_wallpapers(): w = Wallpaper w.time_gap("Arranging Desktop wallpapers") for filename in os.listdir(w.file_urls["wall_dst"]): base_file, ext = os.path.splitext(filename) if ext == ".jpg": try: im = Image.open(w.file_urls["wall_dst"] + filename) if list(im.size)[0] == 1920: im.close() os.rename(w.file_urls["wall_dst"] + filename, w.file_urls["wall_desktop"] + filename) elif list(im.size)[0] == 1080: im.close() os.rename(w.file_urls["wall_dst"] + filename, w.file_urls["wall_mobile"] + filename) else: im.close() except FileExistsError: print("File Already Exists!") os.remove(w.file_urls["wall_dst"] + filename) @staticmethod def exec_all(): w = Wallpaper w.copy_wallpapers() w.change_ext() w.extract_wall() w.arr_desk_wallpapers() print(w.msg) time.sleep(3)wall = Wallpaper()wall.exec_all()
然後直接在控制台 cmd 下,直接輸入 python warllpaper_extract.py
,但可能會發生如下錯誤:
解決方法:找不到模塊 PIL
,我們可以用 pip install Pillow
來安裝:
安裝成功後,再次執行
圖片提取完成,而且能把桌面和手機端的圖片分開,
點 desktop 下,成功看到我們想要的圖片,恭喜你,一旦下次發現了有新的鎖屏壁紙,你就可以運行一下這個 Python 腳本,然後去選自己心儀的照片吧:
至此我們從手動獲取圖片到利用 Python 實現自動化,而且通過本文你可以學習 Python 的 PIL 模塊和靜態方法。好了,我們下一篇文章再見!