Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google reverse image search using POST request

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()
like image 694
Natsukane Avatar asked Apr 24 '14 13:04

Natsukane


People also ask

How do I do a reverse image search and upload it?

On a desktop, computer reverse image search is simple. Just go to images.google.com and click on the little camera icon in the search bar. Now you can either paste in the URL for an image you've seen online, upload an image from your hard drive, or drag an image into the search box.

Can I upload an image to Google to search?

To include a picture in Google search results, add your image to a website along with a description. While you can't directly upload images into search results, searchable images posted on a website can show up in our search results.

Does Google reverse image search work with screenshots?

Yes, of course. Doing a reverse image search of a screenshot on a phone is pretty much like how it is on a desktop. If you are on Android or iPhone, just open your favorite mobile browser, visit images.google.com and click on the camera icon. Then, choose the screenshot you just took and upload it.


1 Answers

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!

like image 54
Uri Granta Avatar answered Oct 16 '22 20:10

Uri Granta