Ppython implements automatic desktop wallpaper change
Speaking of which, brothers, how do you comfort your girlfriend when she is angry?
No way, no way, no one is still single!
Forget it, let’s get back to the topic. Besides, I’m going to get beaten~
Today we are going to talk about how programmers coax their girlfriends. Let’s get started without further ado!
Preparation
1. Environment
First we prepare the environment and editor. I use:
- Python 3.8 interpreter
- PyCharm Editor
2. Modules used
import re # 正则表达式模块
import requests # 数据请求模块
import os # 文件操作模块
import ctypes # 时间模块
request is a third-party module and needs to be installed manually. win + R
Enter cmd
the installation command.pip install requests
Others are built-in, no need to install, just call them directly.
3. How to configure the python interpreter in pycharm?
- Select file >>> setting >>> Project >>> python interpreter
- Click the gear and select add
- Add python installation path
4. How to install plug-ins in pycharm?
- Select file >>> setting >>> Plugins
- Click Marketplace and enter the name of the plug-in you want to install. For example: for translation plug-in, enter translation / for Chinese plug-in, enter Chinese.
- Select the corresponding plug-in and click install.
- After the installation is successful, the option to restart pycharm will pop up. Click OK and the restart will take effect.
Code practice
For the sake of single friends, I divided the case into two parts. I put the source code on the business card at the end of the article. Take it for yourself.
- Get wallpaper
- Set up automatic replacement
Of course, if you are not single, just watch the second part, haha~
1. Get wallpapers
First, let’s get a single wallpaper
There are four steps to get the wallpaper. I will block the following URLs to prevent detection.
- Send a request, for http://www.***.com/desk/31131.htm Send a request
- Get data, get web page source code <server returns response data>
- Parse the data and extract the data content, wallpaper link and name we want
- Save data, save wallpaper file to local file
send request
for page in range(3, 21):
print(f'正在采集第{
page}页的数据内容')
# 请求链接
link = f'http://www.****.com/index_{
page}.htm'
# 发送请求
html_data = requests.get(link).text
# 提取ID
img_id_list = re.findall('<a href="/desk/(\d+)\.htm" title=', html_data)
# for循环遍历
for img_id in img_id_list:
# 请求链接
url = f'http://www.***.com/desk/{
img_id}.htm'
# 发送请求
response = requests.get(url)
retrieve data
response.text 获取响应文本数据
response.encoding = response.apparent_encoding 自动识别码
Analytical data
# 获得到数据, 返回出现乱码了
response.encoding = 'gbk'
# 链接地址 以及 标题
img, title = re.findall('<img src="(.*?)" alt="(.*?)"', response.text)[0]
print(img, title)
save data
# 发送请求, 获取数据
img_content = requests.get(img).content
with open('img\\' + title + '.jpg', mode='wb') as f:
f.write(img_content)
Multiple data collection on multiple pages
I will only give you a general idea here. I have specially recorded a video to explain it. You can get the video on the business card at the end of the article.
One link corresponds to one data. Get multiple links and get multiple data.
http://www.***.com/desk/31095.htm
http://www.***.com/desk/31131.htm
http://www.***.com/desk/31090.htm
http://www.***.com/desk/31092.htm
Second page: http://www.***.com/index_2.htm
The third page: http://www.***.com/index_3.htm
Page 4: http://www.***.com/index_4.htm
Automatically change wallpaper program
import os
import ctypes
import cv2
def Change():
# 壁纸文件夹路径
wallpaper_dir = r"D:\壁纸\output"
# 获取壁纸文件夹中的文件列表
wallpapers = os.listdir(wallpaper_dir)
# 随机选择一张壁纸
# wallpaper_filename = random.choice(wallpapers)
for wallpaper in wallpapers:
# 拼接壁纸文件的完整路径
wallpaper_path = os.path.join(wallpaper_dir, wallpaper)
# 设置壁纸
ctypes.windll.user32.SystemParametersInfoW(20, 0, wallpaper_path, 3)
def convert_mp4_to_jpgs(path):
video_capture = cv2.VideoCapture(path)
still_reading, image = video_capture.read()
frame_count = 5 # 保存的起始帧的编号005
while still_reading:
cv2.imwrite(f"output/frame_{
frame_count:03d}.jpg", image)
# read next image
still_reading, image = video_capture.read()
frame_count += 5 # 保存帧依次增加5,005>>010>>015.......
Change()
at last
In order for you to better let your girlfriend feel your sincerity, I have also prepared a source code for making your girlfriend’s photo into a photo wall. Just get it on the business card below and see the effect.
Okay, this is the end of my sharing about coaxing my girlfriend. I suggest you don’t make your girlfriend angry if it’s okay. If you coax her well, it’s okay. If you can’t coax her, it’ll be fucked!