如何使用python获取Windows 10精美的桌面壁纸

Windows 10界面UI相较于Windows 7做了很多优化,个人最喜欢的就是隔几天自动更新一次的锁屏壁纸。每一张看起来都是大师所做,非常精美。看到打动自已的壁纸,往往想要收藏起来,自定义成桌面壁纸。

在网上搜索发现系统壁纸文件是保存在这样的一个目录中(红色加粗部分每个人的电脑不一样):

C:\Users\cp9_k\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets

目录内容如下:

所有的文件都没有后缀名,如果重命名加上后缀.jpg,使用图片查看器能看到图片。但图片大小尺寸都不一样,有我们想要的电脑壁纸,也有些是尺寸很小的图片,有些是高度大于宽度的(适用于做手机壁纸)的图片。我设置的过滤条件是:

  • 文件大小:大等于200KB
  • 图片方向:宽度大于高度

完整代码(save_win10_wallpapers.py):

# -*- coding: utf-8 -*-
import os
import time
import PIL.Image as Image
from datetime import date

# Usage:
# 1. 配置好文件末尾的ori_path
# 2. 双击脚本


def construct_date(date_string):
    date_args = map(lambda x: int(x), date_string.split("/"))
    return date(date_args[0], date_args[1], date_args[2])


def save_wallpapers(ori_path, des_path, start_date, min_size):
    """
    保存壁纸
    :param ori_path: 源目录地址
    :param des_path: 自定义壁纸目录
    :param start_date: 源目录文件起始日期
    :param min_size: 文件大小最低值,单位为Byte.
    :return: 无
    """
    ctime = time.strftime("%Y%m%d%H%M%S", time.localtime())
    ori_path = ori_path.replace('\\', '/')
    des_path = des_path.replace('\\', '/')
    des_path = des_path + "/" + ctime
    # 创建自义壁纸目录
    if not os.path.exists(des_path):
        os.mkdir(des_path)
    print "ori_path: ", ori_path
    print "des_path: ", des_path

    cmd = 'dir "%s" /tc /od' % ori_path
    print cmd
    count_ = 0
    for line in os.popen(cmd).readlines():  # 按创建时间排序
        split_line = line.split(' ')
        if len(split_line[-1]) < 20:  # 过滤无效的打印内容
            continue
        file_create_date = split_line.pop(0)
        file_size = int(split_line.pop(-2).replace(',', ''))
        file_name = split_line.pop().strip()

        if construct_date(file_create_date) < construct_date(start_date):  # 筛选指定日期及其之后的文件
            continue
        if file_size < min_size:
            continue

        ori_file_full_path = ori_path.replace('/', '\\') + "\\" + file_name
        img = Image.open(ori_file_full_path)
        if img.size[0] < img.size[1]:  # 过滤纵向的壁纸
            continue
        else:
            # 复制文件到目标目录,并加上后缀.jpg
            copy_cmd = "copy %s\%s %s\%s.jpg" % (ori_path.replace('/', '\\'), file_name, des_path.replace('/', '\\'), file_name)
            os.popen(copy_cmd)
            count_ += 1

    print "fetched total %d wallpapers" % count_


if __name__ == "__main__":
    # 系统壁纸存放(源)路径
    ori_path = 'C:\Users\cp9_k\AppData\Local\Packages' \
               '\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets'
    # 自定义的壁纸文件的保存路径
    des_path = "."
    # 文件大小最小值,200kb
    min_size = 200000
    # 只处理大于指定日期的文件
    # start_date = raw_input("set start date(like 2017/09/27):\n>>>")
    start_date = "2017/09/27"

    save_wallpapers(ori_path, des_path, start_date, min_size)
    raw_input("please press Enter to exit.")

使用步骤:

  1. 如果是第一次使用,修改文件底部ori_path, 修改成你自己的电脑的路径
  2. 双击save_win10_wallpapers.py

运行过程如下图:

在当前目录下创建了一个目录20190314113011,壁纸都保存到该目录中,如下图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值