, , , , , , , ,

Monita, my Hispanic maid, twirled my dirty underpants on her finger whilst chewing gum. Whatcha got it doin’, she asked, her hand on hip.

‘It gets images from Google, affording me a slide show with remote control.’

She fetched a handkerchief from her pinny and blew her nose. ‘Why?’ she asked. I had no answer to that.

The Python code begins by utilising the argparse package, handy for dealing with command line arguments. In this case, we will supply our program with a single argument: hats

python slideshow.py -i hats

Since we are using Python Tools for Visual Studio to develop our code, we can right-click the project file and set the command line argument in the Script Arguments box.


import argparse
import requests
from BeautifulSoup import BeautifulSoup
from raswik import Raswik
import webbrowser
from time import sleep

# set up command line argument parser 
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help='input search term', required=True)
args = parser.parse_args()

#get search term
search_term = args.input

Great. Now that we are passing our program a search term, let’s incorporate it into a Google search for hats. We can fetch the webpage using the requests package, and then harness Beautiful Soup to build a list of links to the actual images.

#build url
url = "https://www.google.co.uk/search?q=image+{}&tbm=isch".format(search_term)

#get webpage
request = requests.get(url)
soup = BeautifulSoup(request.text)     

# get image links
images = []
for img in soup.findAll('img'):

This is where the slide show comes in. If we press a button on our RasWIK wireless inventors kit (effectively acting as a remote control), we can grab the next link and display, say, a homburg in the browser. We just keep looping until all our stored links are used up.

# iterate through links
count = 0
while count < len(images):

    # if button pressed...
        # ...then display next image in browser
        webbrowser.open(images[count], new=0 )
        count += 1

I won’t pretend this is perfect. For a start, despite the ‘new=0’ parameter on the webbrowser object, we are still seeing a new browser window opening on the Pi for each image (I’m targeting the Dillo browser as it renders real quick, whereas Chromium kills my Raspberry processor). I’d also rather like to get this working with voice command, but perhaps another day. That said, it got Arkwood’s underpants moist, much to Monita’s annoyance.

As far as the code for the RasWIK is concerned, I thought it best to start wrapping this in its own dedicated class. Just a simple function here, to check if the button on the wireless device has been pressed before displaying the next hat.

import serial
from time import sleep

# class to handle communication to 
# raswik wirerless inventors kit
class Raswik(object):

    # com port and speed
    port = "/dev/ttyAMA0"
    baud = 9600

    #check if a button has been pressed
    def is_button_pressed(self, pin):
        #check pin param
        if(pin == ""):
            return False

        reply = ""
            # establish a serial connection
            ser = serial.Serial(port= self.port, baudrate= self.baud)            

            # wait a mo...

            # write to pin e.g. a--D10READ--
            # wait some more...
            # now read from pin
            reply = ser.read(12)

            # close serial connection 
            print ("Error detecting raswik button press")
            return False

        # grab the bit of the reply we care about
        message = reply[6:].strip('-')

        # return whether button is currently pressed down
        if(message == "LOW"):
            return True
            return False

Well, the upshot of each button press is to display a rather snazzy fedora, or bowler.


And what blog post would be complete without a snap of the RasWIK in action, flashing away like some sordid old pervert in a Mackintosh raincoat.


Of course, you can imagine what Arkwood punched in as his search term. 27 years old going on 10, as he sniggered uncontrollably at the boobs. Monita shook her head in disgust and picked up the washing basket, whilst we fought over the escalating obscenities.