发一波壁纸

win10的锁屏壁纸还挺好看的。鉴于微软自己会更新壁纸,所以还是授人以渔,代码在最后。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 这是python3 代码
# coding : utf-8
'''
为了把win10锁屏壁纸挪到我的onedrive上去
'''
#导入库
import os
import shutil
import time
from hashlib import md5
from PIL import Image as img
import getpass
'''
初始化路径
'''
username = getpass.getuser()
src = os.path.join(r'C:\Users',username,r'AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets')
dst = os.getcwd()
#dst = r'E:\onedrive\图片\wallpaper'
'''
获取当前时间并格式化
'''
def GetTime():
ISOTIMEFORMAT = r'%y-%m-%d--%H-%H-%S'
#ISOTIMEFORMAT = r'%y-%m-%d'
now = time.strftime(ISOTIMEFORMAT, time.localtime())
return now
'''
获取文件的MD5
'''
def md5_file(path):
m = md5()
a_file = open(path, 'rb') #需要使用二进制格式读取文件内容
m.update(a_file.read())
a_file.close()
return m.hexdigest()
'''
获取目标文件中的以string形式存储的dict,并用eval把str转为dict。
'''
def get_existing_files(Md5_file_path):
with open(Md5_file_path,'r') as f:
data = eval(f.read())
return data
'''
获取以list形式发送过来的path,查询其md5值,并以dict返回。
'''
def get_path_files_md5(path):
data = {}
for file in path:
try:
md5_value = md5_file(os.path.join(path,file))
data[md5_value]=file
except:
pass
return data
'''
将大于100k的文件,复制到目标文件夹(我的是onedrive),文件名为 时间
'''
def get_Wallpaper(src,dst):
files = os.listdir(src)
dst_table=os.path.join(dst,'桌面壁纸')
dst_phone=os.path.join(dst,'手机壁纸')
#检查文件夹是否存在,没有就创建
if os.path.exists(dst_table):
pass
else:
os.mkdir('桌面壁纸')
if os.path.exists(dst_phone):
pass
else:
os.mkdir('手机壁纸')
#获取file_list
phone_list = os.listdir(dst_phone)
table_list = os.listdir(dst_table)
file_list = set(table_list+phone_list)
data = get_path_files_md5(file_list)
#正式处理文件
for i in files:
src_filename = os.path.join(src,i)
a , b = os.path.splitext(src_filename)
file_size = os.stat(os.path.join(src,i)).st_size
#说明一下win10的壁纸文件是没有格式的,所以需要甄别,并添加后缀
if file_size > 100000:
#获取图片分辨率
x,y = img.open(src_filename).size
#判断后缀
if b == ".jpg":
pic_md5 = md5_file(os.path.join(src,i))
if data.get(pic_md5) == None:
#由于win10的wallpapaer文件是没有格式的,所以就没有必要做这个判断
#这个功能是给转移其他位置的图片使用的
new_name = GetTime()
if x == 1080:
shutil.move(os.path.join(src,i),os.path.join(dst_phone,'',new_name))
elif x == 1920:
shutil.move(os.path.join(src,i),os.path.join(dst_table,'',new_name))
print("hey guy,i found one wallpaper pic")
data[pic_md5]=new_name
time.sleep(1)
else:
pic_md5 = md5_file(os.path.join(src,i))
if data.get(pic_md5) == None:
new_name = GetTime()+'.jpg'
#复制并改名
if x == 1080:
shutil.copy2(os.path.join(src,i),os.path.join(dst_phone,'',new_name))
elif x == 1920:
shutil.copy2(os.path.join(src,i),os.path.join(dst_table,'',new_name))
data[pic_md5]=new_name
time.sleep(1)
else:
pass
if __name__ == '__main__':
get_Wallpaper(src,dst)

代码里面已经做了文件校验,多次运行也不会复制重复的图进去哦。

win10建议增加一个bat启动py文件,然后再放入计划任务。就可以每天自动去抓去图片啦。

bat 内容参考:

路径自己修改咯

1
python "E:\onedrive\python_codes\my-tools\win10wallpaper.py"