11

I have an app that's basically a database of images stored on my local drive. Sometimes I need to find a higher resolution version or the web source of an image, and Google's reverse image search is ideal for that.

Unfortunately, Google doesn't have an API for it, so I had to figure out a way to do it manually. Right now I'm using Selenium, but that obviously has a lot of overhead. I'd like a simple solution using urllib2 or something similar - send a POST request, get the search URL back, and then I can just pass that URL to webbrowser.open(url) to load it in my already opened system browser.

Here's what I'm using right now:

gotUrl = QtCore.pyqtSignal(str)
filePath = "/mnt/Images/test.png"

browser = webdriver.Firefox()
browser.get('http://www.google.hr/imghp')

# Click "Search by image" icon
elem = browser.find_element_by_class_name('gsst_a')
elem.click()

# Switch from "Paste image URL" to "Upload an image"
browser.execute_script("google.qb.ti(true);return false")

# Set the path of the local file and submit
elem = browser.find_element_by_id("qbfile")
elem.send_keys(filePath)

# Get the resulting URL and make sure it's displayed in English
browser.get(browser.current_url+"&hl=en")
try:
    # If there are multiple image sizes, we want the URL for the "All sizes" page
    elem = browser.find_element_by_link_text("All sizes")
    elem.click()
    gotUrl.emit(browser.current_url)
except:
    gotUrl.emit(browser.current_url)
browser.quit()
Natsukane
  • 611
  • 7
  • 19
  • If you are commercial, TinEye is a good option. If you can upload them somewhere the url `www.google.com/searchbyimage?image_url=IMAGE_URL` would be useful. – Others Feb 27 '15 at 23:06

1 Answers1

17

This is easy to do if you're happy to install the requests module. The reverse image search workflow currently consists of a single POST request with a multipart body to an upload URL, the response to which is a redirect to the actual results page.

import requests
import webbrowser

filePath = '/mnt/Images/test.png'
searchUrl = 'http://www.google.hr/searchbyimage/upload'
multipart = {'encoded_image': (filePath, open(filePath, 'rb')), 'image_content': ''}
response = requests.post(searchUrl, files=multipart, allow_redirects=False)
fetchUrl = response.headers['Location']
webbrowser.open(fetchUrl)

Of course, remember that Google may decide to change this workflow at any point!

Dan Harmon
  • 193
  • 10
Uri Granta
  • 1,410
  • 9
  • 20