使用 Python 获取 Windows 聚焦图片

Windows 聚焦图片会定期更新,拿来做壁纸不错,它的目录是:

%localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\
LocalState\Assets\

这里的文件是没有扩展名的,大于 100 KB 的就是图片,可以直接复制出来加上 .jpg

Python 代码

import os
import shutil

# Windows Focus 图片目录
PATH = os.environ["LOCALAPPDATA"] + \
    "/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/"
# 图片保存目录
TARGET_PATH = "./Windows_Focus_Images/"

def get_windows_focus():
    files = os.listdir(PATH)
    os.makedirs(TARGET_PATH, exist_ok=True)

    for file in files:
        if os.path.getsize(PATH + file) / 1024 > 150:
            # 复制文件并加上扩展名
            shutil.copy(PATH + file, TARGET_PATH + file + '.jpg')

    
if __name__ == "__main__":
    get_windows_focus()

%localappdata% 这种变量在 Windows 资源管理器中可以被识别,但是在 Python 中不能,需要用 os.environ() 函数来获取,将 % 去掉全部改为大写即可。

猜你喜欢

转载自www.cnblogs.com/cloudfloating/p/11856612.html