Google image search: How do I construct a reverse image search URL?
Asked Answered
C

5

12

How can I programmatically through java convert an image to "some string" to pass it as a parameter for searching in google image search. Actually I have made some base64 convertion of image but it differs from that that google does in its image search engine. I've made such a convertion(java 7):

import javax.xml.bind.DatatypeConverter;
...
            Path p = Paths.get("my_photo.JPG");
            try(InputStream in = Files.newInputStream(p); 
                    PrintWriter write = new PrintWriter("base64.txt");
               ) {
                byte [] bytes = new byte[in.available()];
                in.read(bytes);
                String base64 = DatatypeConverter.printBase64Binary(bytes);
                write.println(base64);

            } catch(IOException ex) {
                ex.printStackTrace();
            }

the output of this simple program differs from the google's string in url. I talk about that string that goes after tbs=sbi:AMhZZ...

Carrel answered 28/9, 2011 at 14:26 Comment(7)
so, can I use this google's service in another way. I just simply want to get the code of a page of specified image, like in google image searchCarrel
I don't understand what you are trying to accomplish. Can you give an example?Siskind
i want to use a google image search service as in code.google.com/intl/uk/apis/imagesearch/v1/… but instead of text parameters I want to use image as a parameter(note: I needn't to use json, simply it is using in example)Carrel
Keep in mind that this is an experimental service, you might not want to build an application that depends on it. There are other reverse image search enginesSiskind
Sorry, I was stuck thinking about a 1:1 conversion of an image to a string, not what was actually going on inside the search engine. My new answer should be more helpful.Siskind
There's a [thread on Quora][Q] that goes into some details on image fingerprinting algorithms, especially [this answer][Q2] by someone who claims to have worked on Google's reverse image search feature: > [The project] used SURF, PCA-SIFT for extracting key points > and descriptors (vector of float values) then used LSH for indexing > and matching! The actual algorithm seems to be proprietary, and [possibly even patented][P]? The internal system seems to be called "quimby" for some reason, maybe someone can expand on that? [Q]:quora.com/Algorithms/What-is-the-algorithm-used-by-GooglSchulze
Did you ever find a way to pass a base64 image to Google image search?Crocidolite
S
14

This is my best guess for how the image search works:

The data in the URL is not an encoded form of the image. The data is an image fingerprint used for fuzzy matching.

You should notice that when you upload an image for searching, it is a 2 step process. The first step uploads the image via the url http://images.google.com/searchbyimage/upload. The Google server returns the fingerprint. The browser is then redirected to a search page with a query string based on the fingerprint.

Unless Google publishes the algorithm for generating the fingerprint, you will be unable to generate the search query string from within your application. Until then, you can have your application post the image to the upload URI. You should be able to parse the response and construct the query string.

EDIT

These are the keys and values sent to the server when I uploaded a file.

image_url       =
btnG            = Search
encoded_image   = // the binary image content goes here
image_content   =
filename        =
hl              = en
bih             = 507
biw             = 1920

"bih" and "biw" look like dimensions, but do not corrispond to the uploaded file.

Use this information at your own risk. It is an undocumented api that could change and break your application.

Siskind answered 28/9, 2011 at 16:27 Comment(3)
Thanks, I guess what you are talking about. Can you describe or give an example how I can make a post request with image to that url?Carrel
@maks, I hope my edit is more helpful. You need to encode the key/values as "multipart/form-data" and send it as the POST request body. You should be able to find plenty of examples on how to do the encoding.Siskind
Can you please enlighten more on this...I'm trying to do the same on WindowsPhone7Landing
P
7
Using google's image search.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpFileUpload {
  public static void main(String args[]){
    try {
      HttpClient client = new DefaultHttpClient();
      String url="https://www.google.co.in/searchbyimage/upload";
      String imageFile="c:\\temp\\shirt.jpg";
      HttpPost post = new HttpPost(url);

      MultipartEntity entity = new MultipartEntity();
      entity.addPart("encoded_image", new FileBody(new File(imageFile)));
      entity.addPart("image_url",new StringBody(""));
      entity.addPart("image_content",new StringBody(""));
      entity.addPart("filename",new StringBody(""));
      entity.addPart("h1",new StringBody("en"));
      entity.addPart("bih",new StringBody("179"));
      entity.addPart("biw",new StringBody("1600"));

      post.setEntity(entity);
      HttpResponse response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));         

      String line = "";
      while ((line = rd.readLine()) != null) {
        if (line.indexOf("HREF")>0)
      System.out.println(line.substring(8));
      }

    }catch (ClientProtocolException cpx){
      cpx.printStackTrace();
    }catch (IOException ioex){
      ioex.printStackTrace();
    }
 }
}
Pocketbook answered 21/9, 2013 at 19:47 Comment(1)
only "encode_image" parameter is required, rest parameters are not required.Bedelia
K
4

Based on @Ajit's answer, this does the same but using the curl command (Linux / Cygwin / etc)

curl -s -F "image_url=" -F "image_content=" -F "filename=" -F "h1=en"  -F "bih=179" -F "biw=1600" -F "encoded_image=@my_image_file.jpg" https://www.google.co.in/searchbyimage/upload

This will print a URL on standard output. You can download that URL with curl or wget but you may have to change the User Agent to that of a graphical web browser like Chrome.

Kenn answered 31/10, 2014 at 16:54 Comment(0)
Z
1

This is what work for me. No need any encoding actually.

https://www.google.com/searchbyimage?image_url=YOUR_IMAGE_URL
Zola answered 16/7, 2020 at 16:51 Comment(1)
This is for an image that is uploaded onlineKenn
P
-1

Use Google Vision API for that. There are also lot of examples available from Google

Procter answered 20/3, 2019 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.