Python 3 Unsplash API Script to Change or Set Random Image Desktop Wallpaper Background in Windows or Linux Full Project For Beginners

Python 3 Unsplash API Script to Change or Set Random Image Desktop Wallpaper Background in Windows or Linux Full Project For Beginners

 

Welcome folks today in this post we will be setting random desktop wallpaper image from unsplash api in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started just make an app.py file and copy paste the following code

 

FOR WINDOWS USERS ONLY

 

app.py

 

 

from requests import get
import os
import ctypes
import sys

url = "https://source.unsplash.com/random"
file_name = "random.jpg"

def is_64bit():
    return sys.maxsize > 2 ** 32


def download(url, file_name):
    '''
    downloading the file and saving it
    '''
    with open(file_name, "wb") as file:
        response = get(url)
        file.write(response.content)


def setup(pathtofile):
    name_of_file = pathtofile
    path_to_file = os.path.join(os.getcwd(), name_of_file)
    SPI_SETDESKWALLPAPER = 20
    if is_64bit():
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_file, 0)
    else:
        ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, path_to_file, 0)


if __name__ == "__main__":
    try:
        download(url, file_name)
        setup(file_name)
    except Exception as e:
        print(f"Error {e}")
        raise NotImplementedError

 

See also  Python 3 Tkinter Base Number System Converter (Binary,Hexadecimal,Decimal,IP Address) Using IPAddress Library GUI Desktop App Full Project For Beginners

 

Now if you execute the python script by typing the below command

 

python app.py

 

 

 

 

As you can see in the above desktop wallpaper it has been automatically changed by this python script. First of all it downloads this image from unsplash website and then set it as desktop wallpaper

 

 

FOR LINUX USERS ONLY

 

app.py

 

 

from requests import get  # to make GET request
from os import system, getcwd, path


url = "https://source.unsplash.com/random"
filename = "random.jpg"


def download(url, file_name):
    '''
    downloading the file and saving it
    '''
    with open(file_name, "wb") as file:
        response = get(url)
        file.write(response.content)


def setup(pathtofile):
    '''
    setting the up file
    '''
    system("nitrogen --set-auto {}".format(path.join(getcwd(), pathtofile)))


if __name__ == "__main__":
    download(url, filename)
    setup(filename)

Leave a Reply