How to implement Bing’s Wallpaper of the day functionality using a simple Python script

How to implement Bing’s Wallpaper of the day functionality using a simple Python script

Use python to automatically fetch new wallpapers from unsplash.com and set them as desktop wallpaper

Photo by [Sanni Sahil](https://cdn.hashnode.com/res/hashnode/image/upload/v1622669634279/HKIUFh1Mq.html) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)Photo by Sanni Sahil on Unsplash

Have you ever wanted to build your own version of Bing’s daily wallpaper? Well, using python, you can automate just about any task, including fetching new wallpapers and setting them as your desktop’s background. In this tutorial, we will use Unsplash public API to fetch beautiful wallpapers, save them to your file system, and set them as your desktop background.

So, let’s get started now.

Requirements:

  • Python — 3.8

  • Pip — 19.0

  • requests

  • Pillow

    NOTE: Pillow and PIL cannot survive together. You must uninstall one of them and only install one.

Let’s install our requirements first -

Get the latest version of Python here.

Pip comes packaged with the python distribution.

python3 -m pip install requests

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

UNSPLASH API

BASE API URL https://api.unsplash.com/

API Endpoint -

GET /photos/random

Parameters-

query = wallpaper
orientation = landscape

We will use the Unsplash API to fetch wallpaper images.

  • Go to this link and register as a developer.

  • Create a new application.

  • Copy the access & secret key. Keep them ready.

Now, we are ready to request images from Unsplash.

Let’s run the code below, and inspect the response from unsplash.

import ctypes

from PIL import Image

from io import BytesIO

import requests

r = requests.get('https://api.unsplash.com/photos/random/?client_id=<YOUR_CLIENT_ID>&query=wallpaper&orientation=landscape')

r = r.json()

print(r)

Copy and paste the access key you obtained earlier in place of <YOUR CLIENT ID>.

Here, we send a GET request to /photos/random endpoint, and the response is stored in r. Then, we parse the response into a JSON object using the method .json(). Next, let’s print out our response to inspect its structure. We see the following result:

If you notice closely, we found a links property that contains a child node 'download'. This field contains the link for downloading the image and that is exactly what we need.

So, let’s get the image from the link-

img_link = r["links"]["download"] 
r2 = requests.get(img_link)

Now, what we have is binary data and is stored in r2.content property. We will use BytesIO() method from the io module to read our binary data and open the image using Image.open() from PIL library.

Finally, save the image wherever you want to store the wallpapers.

i = Image.open(BytesIO(r2.content))

save_path = r"C:\Users\ipras\Pictures\Saved Pictures\image" + r["id"] + ".jpg"

i.save(save_path)

Remember to change the save_path to the directory where you want to store the images.

We are already halfway there. Now, we just need to set the downloaded image as our desktop wallpaper.

For that, we have to somehow access the user32.dll file, which is a standard DLL that comes with windows and contains API that helps an app create and implement standard windows GUI.

NOTE: DLL stands for Dynamic link library. As it says on Windows Dev Docs, **A dynamic-link library (DLL) is a module that contains functions and data that can be used by another module (application or DLL). The Windows application programming interface (API) is implemented as a set of DLLs, so any process that uses the Windows API uses dynamic linking.**

user32.dll contains the method SystemParametersInfoW, which lets you access and set system-wide parameters such as SPI_SETDESKTOPWALLPAPERS.

Luckily, in Python, there’s a ctypes library which is a foreign function library for python. It provides C compatible data types and allows you to access functions present in shared libraries or DLLs.

Write the following code in the same file.

SPI_SETDESKWALLPAPER = 20 ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, save_path , 0)

Run the script and you should see the desktop wallpaper change.

And it’s done. Bingo!!!

We have our python script that fetches beautiful wallpapers from Unsplash and sets them as our desktop background.

Here’s the full code :

import ctypes
from PIL import Image
from io import BytesIO
import requests

#Send a get request to unsplash at /photos/random with query='wallpaper' and orientation='landscape' parameters.
r = requests.get('https://api.unsplash.com/photos/random/?client_id=<YOUR_CLIENT_ID>&query=wallpaper&orientation=landscape')
r = r.json()
#Store image download link
img_link = r["links"]["download"]

#Fetch image from link using requests, open and save the image in your file system using Image module from PIL
r2 = requests.get(img_link)
i = Image.open(BytesIO(r2.content))
save_path = r"C:\Users\ipras\Pictures\Saved Pictures\image" + r["id"] + ".jpg"
i.save(save_path)

#Set the image as wallpaper using user32 dll library 
pathToImage = save_path
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, pathToImage, 0)

The only thing remaining is to schedule the python script to run automatically on a daily basis. That can be easily accomplished with the help of the Task Scheduler app on windows.

  • Open task scheduler

  • Click on Create Basic Task…

  • Enter Name and description for the task.

  • Select trigger -

  • Now, select Start a program as an action.

  • Select your script as the action script.

  • Finally, review your task details in the Finish tab and click Finish.

It’s done. Now your python wallpaper changer script will run daily at the specified time and as a result will update your desktop wallpaper.

If you want to learn more about Unsplash API go to their official docs. There are tons of things you can try and experiment with. You can also check out Windows Developer Docs in case you want to learn more about DLLs, user32.dll, and the methods they provide.

Thanks for reading and I hope you found the article interesting.

If you found the article helpful, please like and share it. Follow me on Hashnode to regularly get updates when I publish a new article. You can also connect with me on LinkedIn and check out my Github.

Also, I would appreciate any feedback from you guys.

Originally published at prashantsingh.me