Me on a monday morning: *log's in*
Also me: Ugh! I hate this wallpaper!
Okay, I admit that i'm being too dramatic about this. Although, it isn't really that far from true. I am notorious for constantly getting bored of my wallpaper every few days. Yes, I am one of those people who has to constantly change their wallpaper just to get the day going. My go-to source for wallpapers is Unsplash. I love images that are vibrant and have a lot of minute details. Very original, I know!
While I was on my last wallpaper hunt, I thought of an idea to automate this. By scheduling a change of wallpaper to happen every now and then, I could avoid the otherwise annoying task of hunting for a new one and then setting it. My mom would be so disappointed if she knew i'd become this lazy.
Moving on...here's what I did!
Where do I download the wallpapers from?
Unsplash provides access to their API to anyone who creates an account.
I visited their developers site and registered a new application.
In short, this is what I did:
- Visit: Unsplash Developers and signup.
- Click on Your Apps->New Application.
- Be responsible and blindly accept all the terms and conditions like you always do.
- Put in your Application name and a small description.
- You can apply for production if that's what you are going for. But I would read the Terms and conditions once again before I were you. Otherwise, you might lose access to the API.
- Scroll down to the Access Key and copy it. We'll be needing it for authorisation with the API.
Let's Code!
Step 1: Import all the libraries.
- The requests library helps with issuing GET requests to the Unsplash API. GET requests are used to retrieve data. Which is what we are doing here.
- The urllib.request library allows us to download the image once we have it's URL.
- The ctypes library allows us to interact with windows using python. This gives us the function which allows the script to change the desktop background.
- The random library is used to spit out random numbers.
- The datetime library gives us functions to retrieve the time and date details.
Step 2: Download the wallpaper.
- filters is a list of all the filters which I liked. The code would pick one filter at random and download an image of that filter. This reduced the chances of me getting bored of just one filter.
- per_page is the number of search results I want to be retrieved. With 20 results, it reduces the chances of the same image being retrieved when the a filter is applied more than once.
- client_id is the access key which I'd got when i registered the application on the unsplash api website. This code is for authorisation between my computer with the API when I am querying it for images.
- query_string is the query that is sent to the API. The string is formatted with the filter which is chosen at random by the random.choice function, the number of search results and the access key.
- requests.get function is used to issue a GET request to the API. The result is returned to get_request as a response object.
- data is assigned the json data which is retrieved from get_request using the .json() function.
The structure of the json data in data can be seen as:
The search results can be accessed with the 'results' key of the json data. As set, there are 20 results. Inside each of the 20 results, there are various fields. These fields contain the respective details of that respective image. The attribute that we are interested is in the 'urls' attribute. As we are using the image as a desktop background, I prefer downloading the highest quality image which is in the raw format. The URL to this raw image is accessed with data['results'][0]['urls']['raw']. If you visit the link, you will see that it displays the full image in its highest resolution.
- file_name stores the name of the file it will be saved with on the local machine. This is set as the date when it was downloaded. This would allow for easy search incase I want to use it for another purpose.
- The unsplash API allowing for dynamic resizing of the image. So, i can set a certain number of parameters which will be applied to the image, ensuring that I get the image in the specification that I want. The dynamic_resize variable stores the suffix which will be added to the image url and stored in retrieval_url before the image is downloaded.
A complete list of the possible parameters can be found at API Documentation. - url.urlretrieve(retrieval_url, file_name) downloads the image using the url and stores it in the current working directory with the file_name. </b>
Step 3: Setting the wallpaper
- The SystemParametersInfoW function is a direct Windows interface.
- The SPI_SETDESKTOPWALLPAPER is named as such because this is how it will be referenced in all of windows documentation.
- The pathToImage has the full path to the image which is required for windows to access it.
- The rest of the parameters are certain flags which can be used for resizing and other functionalities.
This script when run, can now change the background of my Windows PC.
How do I get complete automation?
Now what remained was for me to schedule this script to run everyday. This way, it would change the wallpaper of my desktop everyday and I wont have to go hunting for a new one constantly. I did this using the native Windows Task Scheduler. With this, I set my script to run everyday at 09:30AM.
Here's a tutorial if you need help with using the Windows Task Scheduler.
Schedule Python Scripts with Windows Task Scheduler
Thank You!
You have made it to the end of this article. This is how I took care of the menacing task of searching for a new wallpaper every few days.
All my code for this project is in my GitHub Repositoy.
I hope this helps you too!
Happy Coding!