说明

锁屏界面Windows10聚焦所产生的图片很多都是著名摄影作品,很好看,但是Windows并不能将其设置为桌面壁纸,所以需要手动找到其保存位置,修改文件后缀名为jpg就可以保存为图片了,但是这样很麻烦,时间久了再好看的图片也会看腻,写一个脚本用来及时更新图片。

内容

脚本分为两个部分,一个run.bat批处理文件,仅有两行代码,另一个是get_wallpaper.pyPython脚本,是核心代码。

get_wallpaper.py

'''
@Email: rumosky@163.com
@Author: rumosky
@Gitee: https://gitee.com/rumosky_admin
@Date: 2019-10-16 17:11:17
@Description: 一键获取Windows聚焦壁纸并保存为jpg图片脚本
'''
import os
import shutil
source = r"C:\Users\rumosky\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
destination = r"D:\wallpaper"


def get_path(dir):
    for root, dirs, files in os.walk(dir):
        pass
    return root, files
    pass


def get_destination_jpg(dir):
    root, files = get_path(destination)
    destination_jpg = []
    for file in files:
        if(file[-3:] == 'jpg'):
            destination_jpg.append(file)
            pass
        pass
    return destination_jpg
    pass


def find_new_jpg():
    des = get_destination_jpg(destination)
    r, source_files = get_path(source)
    new_jpg = []
    count = 0
    for source_file in source_files:
        if source_file+".jpg" not in des:
            count = count+1
            new_jpg.append(source_file)
            pass
        pass
    return count, new_jpg
    pass


def copy():
    count, new_jpg = find_new_jpg()
    if count > 0:
        for jpg_file in new_jpg:
            source_path = source+'\\'+jpg_file
            destination_path = destination+'\\'+jpg_file+'.jpg'
            shutil.copyfile(source_path, destination_path)
            pass
        pass
    return count, new_jpg
    pass


if __name__ == "__main__":
    count, without = copy()
    new_jpg = []
    print("更新了 "+str(count)+" 张图片")
    for new_jpg in without:
        print(new_jpg+".jpg")
        pass
    pass
  • source后的路径就是Windows10聚焦文件保存的位置,注意修改Users后面的用户名为自己的
  • destination是图片存放的位置,必须提前建立好该文件夹,否则会出错

run.bat

python get_wallpaper.py
pause

run.bat可以不用,只需要电脑上有能运行Python脚本的软件即可,run一下get_wallpaper.py就行

结果

运行一下run.bat,结果如图:
结果

大功告成!

Last modification:March 6th, 2020 at 01:03 pm
^_^您的每一分钱,都是对我最大的鼓励!