Weekly letter pythonprogramming.email
Hi,
To get the curated list of awesome python articles from all over the Internet, please subscribe with pythonprogramming.email. This is specifically suitable for beginners.
Advertise with us
image script   0   5648
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   5648

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 1: Convert ebooks from epub to mobi format
Python script to convert the ebooks from one format to another in bulk, Automated book conversion to kindle format, Free kindle ebook format conversion, automating the book format conversion, python code to book format convert,...
Python Script 9: Getting System Information in Linux using python script
Finding linux system information like processor details, memory usage and average load using python script. Python program to find Ubuntu information....
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....
SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap