Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
image script   0   14992
Python Script 17: Setting bing image of the day as desktop wallpaper


Using this python script, we will try to download the bing image of the day and set it as desktop wallpaper.

There is a URL which bing itself uses to fetch the image details using AJAX. We will send GET request to this URL to fetch image details.

# get image url
response = requests.get("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
image_data = json.loads(response.text)


Response return will be in below format.

{
	"images": [{
		"startdate": "20190823",
		"fullstartdate": "201908230700",
		"enddate": "20190824",
		"url": "/th?id=OHR.FarmlandLandscape_EN-US6661316442_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
		"urlbase": "/th?id=OHR.FarmlandLandscape_EN-US6661316442",
		"copyright": "Farmland in Washington state's Palouse region (© Art Wolfe/Getty Images)",
		"copyrightlink": "https://www.bing.com/search?q=palouse+region&form=hpcapt&filters=HpDate%3a%2220190823_0700%22",
		"title": "Harvest time in the Palouse",
		"quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20190823_FarmlandLandscape%22&FORM=HPQUIZ",
		"wp": true,
		"hsh": "44238c657b35ce3c11fbf3f59881474e",
		"drk": 1,
		"top": 1,
		"bot": 1,
		"hs": []
	}],
	"tooltips": {
		"loading": "Loading...",
		"previous": "Previous image",
		"next": "Next image",
		"walle": "This image is not available to download as wallpaper.",
		"walls": "Download this image. Use of this image is restricted to wallpaper only."
	}
}


Get the image URL from the response JSON and download it.

# download and save image
img_data = requests.get(full_image_url).content
with open(image_name, 'wb') as handler:
handler.write(img_data)


The image will be saved in the present working directory. Set this image as desktop background using below command.

`which gsettings` set org.gnome.desktop.background picture-uri file:$PWD/imagename

This command will work for Ubuntu. For other operating systems, please update accordingly.


The script is available at Github.


You can automate the process by scheduling this script to run on system startup.


Complete Script:

#!/usr/bin/python

"""
Python script to set bing image of the day as desktop wallpaper
OS: Ubuntu 16.04
Author: Anurag Rana
More Info: https://www.pythoncircle.com
"""

import datetime
import json
import os

import requests

# get image url
response = requests.get("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
image_data = json.loads(response.text)

image_url = image_data["images"][0]["url"]
image_url = image_url.split("&")[0]
full_image_url = "https://www.bing.com" + image_url

# image's name
image_name = datetime.date.today().strftime("%Y%m%d")
image_extension = image_url.split(".")[-1]
image_name = image_name + "." + image_extension

# download and save image
img_data = requests.get(full_image_url).content
with open(image_name, 'wb') as handler:
handler.write(img_data)

# ubuntu command to set wallpaper
os.system("`which gsettings` set org.gnome.desktop.background picture-uri file:$PWD/" + image_name)



image script   0   14992
0 comments on 'Python Script 17: Setting Bing Image Of The Day As Desktop Wallpaper'

Related Articles:
How to validate an uploaded image in Python Django
Validating image before storing it in database or on file storage. Checking the size of uploaded image in python django. Checking the extension of uploaded image in python django. Checking the content size of uploaded image. Checking the mime-type of uploaded Image. Validating a malicious image before saving it. Using python-magic package...
Text to QR code image and QR code image to text generator in Python Django
QR code image to text generator in Python Django, Text to QR code image generator in Python Django, Implementing QR code generator in Python Django. Serving static files in Django media...
How to compress the uploaded image before storing it in Django
Compressing an image in Django before storing it to the server, How to upload and compress and image in Django python, Reducing the size of an image in Django, Faster loading of Django template, Solving cannot write mode RGBA as JPEG error,...
Python program to convert Linux file permissions from octal number to rwx string
Python program to convert Linux file permissions from octal number to rwx string, Linux file conversion python program, Python script to convert Linux file permissions...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap