一、功能简介
1、利用爬虫技术从网络上爬取图片
2、将图片转换为.bmp扩展名类型的图片
3、将图片设置为桌面壁纸
4、打包成exe
5、修改注册表创建右键快捷方式
6、制作卸载程序,删除下载的图片,删除右键快捷方式
二、开始制作啦
直接贴代码,注解或在代码中标明!!!
1、request模块:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3)
2、PIL模块:是Python平台事实上的图像处理标准库,支持多种格式,并提供强大的图形与图像处理功能
3、pywin32是一个第三方模块库,主要的作用是方便python开发者快速调用windows API的一个模块库。注意这里安装的是pypiwin32,因为安装pywin32导入
import win32api, import win32con ,import win32gui 会出现红线,提示找不到对应的模块(?我也不知道为什么,所以就改装pypiwin32,希望有大佬指点一下)
# encoding: utf-8
# pip install requests ---request安装命令
import requests #
import json
import random
import os, sys
import ctypes
# pip install pillow ---PIL 安装命令
from PIL import Image
# pip install pypiwin32 ---win32api、win32con、win32gui安装命令
import win32api
import win32con
import win32gui
# 下载图片
def download_picture():
search_url = 'https://unsplash.com/napi/search?client_id=%s&query=%s&page=1'
client_id = 'fa60305aa82e74134cabc7093ef54c8e2c370c47e73152f72371c828daedfcd7'
categories = ['nature', 'flowers', 'wallpaper', 'landscape', 'sky']
search_url = search_url % (client_id, random.choice(categories))
response = requests.get(search_url)
# 将json格式的字符转换为dict
data = json.loads(response.text)
results = data['photos']['results']
# 利用filter筛选宽高比大于等于1.33的图片
results = list(filter(lambda x: float(x['width']) / x['height'] >= 1.33, results))
# random.choice(results)随机获取一张图片地址
result = random.choice(results)
result_id = str(result['id'])
result_url = result['urls']['regular']
# 以上内容根据不同的网址,写法不同,主要是获取图片的地址
root = 'D:\\wallpaper\\' # 保存图片路径
if not os.path.exists(root):
os.mkdir(root)
jpg_file = root + result_id + '.jpg'
bmp_file = root + result_id + '.bmp'
r = requests.get(result_url)
r.raise_for_status()
with open(jpg_file, 'wb') as file:
file.write(r.content)
img = Image.open(jpg_file)
img.save(bmp_file, 'BMP')
# 将jpg文件删除
os.remove(jpg_file)
return bmp_file
def replace_wallpaper():
bmp_file = download_picture()
# 设置壁纸 图片格式必须为BMP(可是我测试时,没有这个限制啊!只要是一般的格式都能设置成功)
# 方法一:
key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(key, "WallpaperStyle", 0, win32con.REG_SZ, "2")
# 2:拉伸适应桌面;0:桌面居中
win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, bmp_file, 1 + 2)
# 关闭打开的键
win32api.RegCloseKey(key)
# 方法二:
# ctypes.windll.user32.SystemParametersInfoW(20, 0, bmpFile, 0) # 设置桌面壁纸
# 操作注册表,创建右键快捷方式
def set_reg():
# full_path = sys.argv[0]
# 获取exe文件自身的路径
full_path = sys.executable
reg_root = win32con.HKEY_CLASSES_ROOT
# 键的路径(具体路径自行修改)
reg_path = "Directory\\Background\\shell\\wallpaper"
reg_path_children = reg_path + "\\command"
# 权限和参数设置
reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS
try:
# 判断注册表中键值是否存在
key = win32api.RegOpenKeyEx(reg_root, reg_path, 0, reg_flags)
except Exception as e:
print(e)
key = None
key_flag = False
if key is None:
key_flag = True
else:
key_flag = False
if key_flag:
try:
# 也许有人觉得上面判断键是多此一举,我也是新手,我觉得判断一下会好点
# 直接创建(若存在,则为获取)
# 创建父项wallpaper
key, _ = win32api.RegCreateKeyEx(reg_root, reg_path, reg_flags)
win32api.RegSetValueEx(key, '', 0, win32con.REG_SZ, '更换壁纸')
win32api.RegSetValueEx(key, 'Icon', 0, win32con.REG_SZ, full_path)
win32api.RegCloseKey(key)
# 创建子项command
key, _ = win32api.RegCreateKeyEx(reg_root, reg_path_children, reg_flags)
win32api.RegSetValueEx(key, '', 0, win32con.REG_SZ, full_path)
win32api.RegCloseKey(key)
except Exception as e:
win32api.RegCloseKey(key)
ctypes.windll.user32.MessageBoxW(0, '设置设置失败,为什么啊!!!', '右键快捷菜单设置(更换壁纸)', 0)
else:
value, key_type = win32api.RegQueryValueEx(key, 'Icon')
win32api.RegCloseKey(key)
if value != full_path:
key, _ = win32api.RegCreateKeyEx(reg_root, reg_path, reg_flags)
win32api.RegSetValueEx(key, 'Icon', 0, win32con.REG_SZ, full_path)
win32api.RegCloseKey(key)
key, _ = win32api.RegCreateKeyEx(reg_root, reg_path_children, reg_flags)
win32api.RegSetValueEx(key, '', 0, win32con.REG_SZ, full_path)
win32api.RegCloseKey(key)
if __name__ == "__main__":
replace_wallpaper()
set_reg()
# pyinstaller -w -F -i blueWhaleLogo15.ico wallpaper.py
完成后,使用pyinstaller打成exe文件,使用命令:
pyinstaller -w -F -i blueWhaleLogo15.ico wallpaper.py
-F 打成的是单独的exe文件不带dll等其他依赖文件,
-i 给exe添加图标,图标是blueWhaleLogo15.ico
其他就没什么了
运行之后就是这个样子
卸载程序
功能:
1.删除下载的图片及文件夹
2.删除注册表内容,去除右键的快捷方式
# encoding: utf-8
import os, sys
# pywin32
import win32api
import win32con
# pip install pygubu
from tkinter import *
import ctypes
def delete_picture(root):
try:
ls = os.listdir(root)
ls_length = len(ls)
if ls_length == 0:
os.rmdir(root)
else:
for i in ls:
c_path = os.path.join(root, i)
if os.path.isdir(c_path):
delete_picture(c_path)
else:
os.remove(c_path)
os.rmdir(root)
except Exception as e:
print(e)
def delete_reg():
reg_root = win32con.HKEY_CLASSES_ROOT
# 键的路径(具体路径自行修改)
reg_path = "Directory\\Background\\shell\\wallpaper\\command"
# 权限和参数设置
reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS
try:
# 判断注册表中键值是否存在
key = win32api.RegOpenKeyEx(reg_root, reg_path, 0, reg_flags)
except Exception as e:
print(e)
key = None
key_flag = False
if key is None:
key_flag = False
else:
key_flag = True
win32api.RegCloseKey(key)
if key_flag:
try:
# 删除值(key也有close方法,可以用with结构)
# with win32api.RegOpenKeyEx(reg_root, reg_path, 0, reg_flags) as key:
# win32api.RegDeleteValue(key, 'test_value')
# 删除键(需要获取其父键,通过父键删除子键)
reg_parent, subkey_name = os.path.split(reg_path)
try:
key2 = win32api.RegOpenKeyEx(reg_root, reg_parent, 0, reg_flags)
except Exception as e:
print(e)
key2 = None
key_flag2 = False
if key2 is None:
key_flag2 = False
else:
key_flag2 = True
if key_flag2:
win32api.RegDeleteKeyEx(key2, subkey_name)
win32api.RegCloseKey(key2)
reg_parent2, subkey_name2 = os.path.split(reg_parent)
try:
key3 = win32api.RegOpenKeyEx(reg_root, reg_parent2, 0, reg_flags)
except Exception as e:
print(e)
key3 = None
key_flag3 = False
if key3 is None:
key_flag3 = False
else:
key_flag3 = True
if key_flag3:
win32api.RegDeleteKeyEx(key3, subkey_name2)
win32api.RegCloseKey(key3)
else:
win32api.RegCloseKey(key3)
pass
else:
pass
except Exception as e:
print(e)
sys.exit()
if __name__ == "__main__":
delete_reg()
delete_picture('D:\\wallpaper\\')
ctypes.windll.user32.MessageBoxW(0, '删除成功', '右键快捷菜单设置(更换壁纸)', 0)
# pyinstaller -w -F -i blueWhaleLogo15.ico unwallpaper.py
-------------------------------------源码地址-------------------------------------
CSDN下载
GitHub地址