该程序是我学习Python的第一个小程序,用于熟悉Python语法,其中借鉴了很多其他网友的Code,望谅解。
参考列表
主要参考博客列表如下:
- LittleBearLi http://blog.csdn.net/u011584748/article/details/51377915
- http://www.itwendao.com/article/detail/362278.html
简介
本程序主要实现:抓取Bing的图片,并设置为桌面背景,设置该程序在开机时自动运行,便能在开机之后自动更改桌面背景。
主要有如下步骤:
- 从Bing抓取每日图片,保存到本地;
- 将抓取到的.JPG转换成.BMP格式;
- 将.BMP图片设置为桌面背景;
- 设置该程序在开机时自动运行。
前期准备工作
- 下载Python3(本例中使用python3.6.3),并设置环境变量
- 安装Pillow,该插件用于将.JPG转换为.BMP。建议使用pip进行安装,Python3中自带pip,只需在命令行中输入如下命令:
pip install pillow - 安装pywin32,该插件用于将.BMP图片设置为桌面背景。pywin32下载路径https://sourceforge.net/projects/pywin32/?source=navbar
Code
File1:GetBingPicture.py,用于从Bing抓取图片,并按照图片的日期保存到本地
#!/usr/bin/python3
#-*-coding:utf-8-*-
# Filename: GetBingPicture.py
import os
from urllib import request
var_BingPic_Dir = "F:\\flege\\Pictures\\BingPicture";
var_CurrentDayIndex = 0;#If the index is 0,it will get the pic js string of current day; If the index is 1,it will get the pic js string of last day.
var_Bing_Base_Url = "http://cn.bing.com";
var_BingPic_Url = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=" + str(var_CurrentDayIndex) + "&n=1&nc=1361089515117&FORM=HYLH1";
#Function: Get current day from js(year.month,day)
#In: The full js string
#Out: The string of current day
#Author: Flege
#Date: 2017.08.25
def GetStr_CurrentDay(jsStr):
print("GetStr_CurrentDay func:%s"%jsStr);
jsStr = str(jsStr);
idx = jsStr.index("startdate");
print("The current day in js string index is: %d"%idx);
currentDayStr = jsStr[(idx + len("startdate") + 3):((idx + len("startdate") + 3) + 8)];
print(currentDayStr);
#currentDayStr = "20180000";
return currentDayStr;
#Function: Create a directory to storage bing pictures
#In: The path of the directory
#Out: The path of the directory
#Author: Flege
#Date: 2017.08.25
def Create_Dir_Storage_Pic(path):
path = path.strip();#except the space
if 0 == len(path):
return None;
if not os.path.exists(path):
os.makedirs(path,exist_ok=True);
print("Create directory success,the pass is:%s"%(path));
else:
print("The directory is exist!");
return path;
#Function: Get the path to storage the pic
#In: The full js string
#Out: The full path to storage the pic
#Author: Flege
#Date: 2017.08.25
def Get_Path_to_Storage_Pic(jsStr):
varPicPath = Create_Dir_Storage_Pic(var_BingPic_Dir);#Create file directory
varPicPath += "\\" + GetStr_CurrentDay(jsStr);#Get the full path
return varPicPath;
#Function: Parser the pic url from js string
#In: The full js string
#Out: The url of pic
#Auhor: Flege
#Date: 2017.08.25
def Parser_Pic_Url(jsStr):
jsStr = str(jsStr);
print("Parser_Pic_Url fun:%s"%jsStr);
idx1 = jsStr.index("url");
idx2 = jsStr.index("urlbase");
print(idx1);
print(idx2);
outStr = jsStr[(idx1 + 6):(idx2 - 3)];
print("The url of pic is:%s"%outStr);
return outStr;
#Function: Get pic type
#In: The full url of pic
#Out: The string of pic type
#Auhor: Flege
#Date: 2017.08.25
def Get_Pic_Type(picUrl):
picUrl = str(picUrl);
inLen = len(picUrl);
print(inLen);
typeIndex = picUrl.index(".",inLen - 5,inLen);
print("Get_Pic_Type fun:%s"%picUrl);
print(typeIndex);
type = picUrl[typeIndex:];
#type = ".jpg";
return type;
#Function: Get the pictures by the given url and storage the pic to local
#In: The url to get the bing pic js string
#Out: None
#Author: Flege
#Date: 2017.08.25
def Get_Pic():
url = var_BingPic_Url;
print("Get pictures from: %s"%url);
webdata = request.urlopen(url);
jsStr = webdata.read();
#print("Get_Pic func:%s"%jsStr);
picStoragePath = Get_Path_to_Storage_Pic(jsStr);
picUrl = var_Bing_Base_Url + Parser_Pic_Url(jsStr);
print(picUrl);
picType = Get_Pic_Type(picUrl);
print("The pic type is:%s"%picType);
picFullPath = picStoragePath + picType;
print("The pic full path is:%s"%picFullPath);
try:
request.urlretrieve(picUrl,(picStoragePath + picType));
print("Storage file success!");
#os.system("pause");#For Debug
return picFullPath;
except IOError:
print("Storage file failed!!");
os.system("pause");
return None;
return None;
if __name__ == "__main__":
Get_Pic();
File2:SetWindowsWallpaperByStealBing.py,将.JPG转换成.BMP格式,并设置为当前桌面
#!/usr/bin/python3
#-*-coding:utf-8-*-
# Filename: SetWindowsWallpaperByStealBing.py
import os
import win32gui
import win32con
from PIL import Image
import GetBingPicture
varStorageBMPPath = "F:\\flege\\Pictures\\BMP\\";
#Function: Set windows wallpaper by the given .BMP file path
#In: The full path of .BMP file
#Out: None
#Auhor: Flege
#Date: 2017.08.26
def SetWindowsWallpaper(bmpFilePath):
print("The pic path which will be set as wallpaper is:%s"%bmpFilePath);
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,bmpFilePath, win32con.SPIF_SENDWININICHANGE);#Only .BMP file can be set as the wallpaper
return None;
#Function: Get the pic name from the full path of this pic
#In: The full path of the pic;
# The length of the pic type
#Out: The pic name
#Auhor: Flege
#Date: 2017.08.26
def Get_PicName(picPath,typeLen):
picName = picPath[(len(picPath)- typeLen - 8):(len(picPath) - typeLen)];
print("Pic name is:%s"%picName);
return picName;
#Function: Conver the pic to .BMP file from the given pic path, and storage the .BMP file to the disk
#In: The full path of pic
#Out: The full path of .BMP file
#Auhor: Flege
#Date: 2017.08.26
def ConvertPicTypeToBMP(picPath):
print("Convert the pic type to .BMP");
im = Image.open(picPath);
print("Format:%s,Size:%s,Mode:%s"%(im.format,im.size,im.mode));
picName = Get_PicName(picPath,len(str(im.format)));#Get the pic name
bmpPath = varStorageBMPPath + picName + ".BMP";#Set the full path of .BMP file to storage it
print("BMP path:%s"%bmpPath);
im.save(bmpPath);#Save the .BMP file to disk
return bmpPath;
if __name__ == "__main__":
print("This is SetWindowsWallpaper.py");
try:
bmpFullPath = ConvertPicTypeToBMP(GetBingPicture.Get_Pic());#Conver the pic to .BMP,and return the .BMP path
SetWindowsWallpaper(bmpFullPath);#Set windows wallpaper by the given pic path
#os.system("pause");#For Debug
except IOError:
print("Set windows wallpaper failed!");
os.system("pause");
Last Step
最后的步骤便是将SetWindowsWallpaperByStealBing.py设置为开机自动运行。
打开注册表路径HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run新建字符串值,设置数值数据为SetWindowsWallpaperByStealBing.py的绝对路径即可。
成果展示
存储的.BMP文件截图
桌面背景