1000 Python Questions
Get 1 Python question daily. Join this telegram channel https://t.me/python1000questions
Advertise with us
image script   0   6807
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   6807

Related Articles:
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 Script 8: Validating Credit Card Number - Luhn's Algorithm
Validating credit card number using Luhn' Algorithm, Verifying Credit and Debit card using python script, Python code to validate the credit card number, Luhn' algorithm implementation in Python...
Python Script 5: How to find most popular technologies on Stackoverflow
How to find most popular technology on stackoverflow by crawling the stackoverflow site using python. Using python beautifulsoup to crawl web pages on stackoverflow. Python code to crawl stackoverflow, crawling stackoverflow for tags, python script to fetch data from stackoverflow....
Uploading a file to FTP server using Python
Uploading files to FTP server using Python, Python script to connect to ftp server, Python code to login to FTP server and upload file, How to connect to FTP server using python code, ftplib in python, Get server file listing using ftplib in python...
SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap