Review past
Python implementation automatically monitors Github projects and opens web pages
Python implements automatic file categorization
The Python implementation helps you choose the double ball number
Preface:
Today we use a Python script to change “Bing image” to “desktop wallpaper” every day. Directly open the whole ~
Results show
Train of thought to sort out
1, through the web page, get the address of the picture
2, save the image to the absolute path
3, set the absolute path to the image as the wallpaper
4, batch wallpaper automatic switch
The modules needed are as follows:
import urllib.request
import requests
import os.path
import ctypes
Copy the code
First, get the address of the picture
This function gets the final IMG address of the image of the day, based on bing’s web page address, primarily through the requests module.
# request page, jump to the final IMG address
def get_img_url(raw_img_url="https://area.sinaapp.com/bingImg/") :
r = requests.get(raw_img_url)
img_url = r.url # Get the url of the image file
print('img_url:', img_url)
return img_url
Copy the code
Second, save the image to local
This function saves the image to a directory of your choice and returns the absolute address of the current directory.
def save_img(img_url, dirname) :
Save the image to the disk folder dirname
try:
if not os.path.exists(dirname):
print('Folders', dirname, 'Nonexistent, re-established')
# os.mkdir(dirname)
os.makedirs(dirname)
Get the file name of the image, including the suffix
basename = "bing.jpg"
Join the directory with the file name to get the image path
filepath = os.path.join(dirname, basename)
Download the image and save it in a folder
urllib.request.urlretrieve(img_url, filepath)
except IOError as e:
print('File operation failed', e)
except Exception as e:
print(Error:, e)
print("Save", filepath, "successfully!")
return filepath
Copy the code
Third, set the absolute path to the image as the wallpaper
Set the image as desktop wallpaper using the absolute path of the image you obtained earlier.
def set_img_as_wallpaper(filepath) :
ctypes.windll.user32.SystemParametersInfoW(20.0, filepath, 0)
Copy the code
Fourth, run the code’s main function
def main() :
dirname = "D:\\bingImg" # Location where the image is to be saved
img_url = get_img_url()
filepath = save_img(img_url, dirname) # Path to the image file
set_img_as_wallpaper(filepath)
Copy the code
How it works (at this point, you can go back to the top left image in the first image of this article)
Fifth, batch automatic replacement wallpaper
In this case, you can create a batch file named py_bingying. Bat in the same directory as the Python script.
@echo off
del g:\bingImg\*.jpg
python SetBingImgAsWallpaper.py
Copy the code
The second line deletes the bing images downloaded the day before before running the Python script, thus realizing the daily cleaning of old wallpaper and minimizing storage space. The third behavior is to run the Python script above.
How to achieve automatic wallpaper switch, here using the boot run above the batch process method.
Copy the batch file created above, go to the directory below, right-click – paste as a shortcut. In this way to achieve the boot start batch procedures, automatically clear and update wallpaper.
C: User/yourname/AppData/Roaming/Microsoft/Windows/Folder/Folder/Folder/Folder/Folder/Folder/Folder/Folder/Folder/Folder/Folder Set the batch program to run at a specified time each day.
That’s the end of this article. Thanks for watching. The next article in this series of Useful Scripts in Python will be about bulk watermarking in Python
To thank the readers, I’d like to share some of my recent programming gems to give back to each and every one of you.
Dry goods are mainly:
① More than 2000 Python ebooks (both mainstream and classic)
Python Standard Library (Chinese version)
(3) project source code (forty or fifty interesting and classic practice projects and source code)
④Python basic introduction, crawler, Web development, big data analysis videos (suitable for small white learning)
⑤Python Learning Roadmap
⑥ Two days of Python crawler boot camp live permissions
All done ~ see profile or private message for complete source code.