Loading...
Python Automation

Python Automation – Automate wallpaper changes for Linux and Windows directly from Google Image search results. Download HD wallpapers automatically in one click.

— Python Automation —

Do you love customizing your desktop with cool new wallpapers daily such as Joker above?

Are you searching for free wallpaper API all along?

Are you bored of monotonous wallpapers provided by Bing or any other software you are using?

If yes, you are at right place, I have written simple web crawler using python automation which takes search string from you, searches google images for latest high resolution photos and set it as your wallpaper. I have modified it with minor changes so that you can also download first 100 high resolution pictures for particular search term in your computer, instantly! (well, that really depends how fast your internet connection is)

I have written simple explanation of how script works below, but if you are not interested in technical stuff and just get started with it, go to my Github Repo and follow the instructions there. I assume you know basic set up of python and environment variables in your computer. So let’s get started with this simple python automation script.

Before going in code, have a look what you will achieve at the end of this article.

External modules required.

from urllib2 import Request, urlopen   #for making network request
from bs4 import BeautifulSoup  #parsing html
import json  #who don't love json?
from random import randint #so that you will get random photos every time
import imghdr #making sure you get valid jpeg
import Wallpaper #special class for changing wallpaper on your system
import argparse #who don't love argumentised scripts? I do.

Making sure we have everything before running the script. Mandatory argument is the search string (computer ain’t got no brain for knowing what’s in your mind), optionally you can give file path where downloaded image needs to be stored.

 
#if file path given, use that, or else use the directory in which script is being run
if args.filepath:
print "File path given"
PHOTO_PATH = args.filepath 
else:
import os
PHOTO_PATH = os.path.dirname(os.path.abspath(__file__)) + "/photo.jpg"

if args.search_string:
SEARCH_NAME = args.search_string
else:
print "Give search string. Usage : changewall.py \"Search String\""
exit()

 

Preparing google search URL. (If you don’t see proper URL below, refer to original source code here.)

 

#Replace search string with + in place of whitespace
SEARCH_NAME +=" HD DESKTOP WALLPAPER"; #for getting nice HD desktop wallpapers in result
SEARCH_NAME = SEARCH_NAME.replace(' ','+')
print(SEARCH_NAME)

#prepare google search url
SEARCH_URL = "https://www.google.co.in/search?q={}source=lnms&tbm=isch&tbs=isz:ex,iszw:1920,iszh:1080".format(SEARCH_NAME)  #notice 1920*1080, change if you want something else
print(SEARCH_URL)

 

Got the URL, now request google to provide html of image search result page. And we will make sure that google won’t know we are doing it by script by spoofing user agents header in http request.



#manipulate user agent so as to make them believe we are just normal human downloading some image
hdr = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

#make a request and get html in beautiful suop
#req = urllib2.Request(SEARCH_URL,headers=hdr)
req = Request(SEARCH_URL)
req.add_header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
page = urlopen(req).read()
soup = BeautifulSoup(page, 'html.parser')

 

Now the part where magic will happen. We will receive 100 images in first html page which we are going to use to randomly select one and download it. Loop is set to make sure we get valid jpeg.


while invalid_jpeg: 
for child in soup.find("div", {"data-ri":"{}".format(str(randint(0,99)))}).find("div", {"class":"rg_meta"}).children:  
data_content = json.loads(child) 
LINK = data_content["ou"] 
#dowload the phot
print(LINK)  
res = requests.get(LINK, headers=hdr) with open(PHOTO_PATH, 'wb') as W: 
W.write(res.content) 

#check if photo is valid, if not, try another photo, keep trying until you get one 
invalid_jpeg = (imghdr.what(PHOTO_PATH)!="jpeg")

 

Last part, set it as wallpaper. Currently script support Ubuntu unity and windows 10. Check if it works for your computer, if not, open issue on github. Using wallpaper class for setting wallpaper, go and check here for code –> Wallpaper Changer – Amit Bhandari


#set as wallpaper
wallpaper = Wallpaper.Wallpaper()
wallpaper.change(PHOTO_PATH)

Liked my work? You might as well like this cool music player for Android I have developed using which you can get lyrics and artist information on the go and can even access them while off line for free.  — AB Music —

Want to see some of result I got on running this python automation script on my computer, here they are.

For search string ‘Joker’

 

Python Automation

For search string ‘Aston Martin’

Python Automation

For search string ‘megan fox’

Python Automation

 

Python Automation

For search string ‘Angelina Jolly’

Python Automation

Still reading this interesting python automation article? You would love my other articles as well.

One click subtitle downloader

Most creative resume you will ever come across for free

My five article series on writing android application with great architecture

15 comments
  1. Wiley Sumaran

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove people from that service? Appreciate it!

  2. quality mens watches

    I’m amazed, I have to admit. Seldom do I come across a blog that’s both educative and
    interesting, and without a doubt, you’ve
    hit the nail on the head. The issue is an issue that too few men and women are speaking intelligently about.
    I am very happy that I stumbled across this in my hunt for
    something regarding this.

Leave a Reply

Your email address will not be published. Required fields are marked *