> ## Content Index
> Fetch the complete content index at: https://serpapi.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Scrape Google Reverse Images Results using SerpApi
- URL: https://serpapi.com/blog/using-google-reverse-images-api/
- Published: 2022-08-16T18:14:05.000Z
- Updated: 2023-01-31T08:48:33.000Z
- Description: This blog post is about scraping Google Reverse Images results using SerpApi.
- Author: Artur Chukhrai
- Tags: Google Reverse Images, Web Scraping, Python

## What is Google Reverse Images

To put it simply, it helps you quickly discover visually similar images from around the web. Your user can upload a photo from your desktop/mobile, or paste a URL of a photo, and it will show you related images used on other websites and also different sizes of the same photo almost instantly.

For example, you can take (paste URL) a photo of a minecraft pillow and use it to search for info or other similar images.

The results can include:

- Search results for objects in the image.
- Similar images.
- Websites with the image or a similar image.

In this blog post, we'll show how you can utilize SerpApi ability to parse data from reverse images results. We're doing it without browser automation, which is a lot faster.

Example of results that are being returned from Google based on a given image:

![blog-google-reverse-image](https://user-images.githubusercontent.com/81998012/184943258-41261bf6-1749-4378-820c-abee500d9b8f.png)

The typical reverse image process:

![illustration of reverse image process](https://user-images.githubusercontent.com/78694043/182384089-a24df49f-a0e4-452c-80e9-cb027d23cba2.png)

## Simple Hello World

```python
from serpapi import GoogleSearch
import os, json

image_url = "https://user-images.githubusercontent.com/81998012/182214192-59dfb3fe-522c-4979-bb42-9f8091dfd9d6.jpg"

params = {
    "api_key": "...",                    # https://serpapi.com/manage-api-key
    "engine": "google_reverse_image",    # SerpApi search engine
    "image_url": image_url,              # image URL to perform a reverse search
    "hl": "en",                          # language of the search
    "gl": "us"                           # country of the search
    # other parameters
}

search = GoogleSearch(params)           # where data extraction happens on the SerpApi backend
results = search.get_dict()             # JSON -> Python dictionary

# ["image_results"] is basically a Google organic results
print(json.dumps(results["image_results"], indent=4, ensure_ascii=False))

```

## Detailed Code

```python
from serpapi import GoogleSearch
import json

image_urls = [
    "https://user-images.githubusercontent.com/81998012/182214192-59dfb3fe-522c-4979-bb42-9f8091dfd9d6.jpg",
    "https://user-images.githubusercontent.com/81998012/182025185-27df7683-24d5-4747-904b-9f3a6045705b.jpg",
    "https://user-images.githubusercontent.com/81998012/182025195-fec95c5c-aee1-448b-9165-ce9dc1b77a56.jpg",
    "https://user-images.githubusercontent.com/81998012/182027073-4b09a0b7-ec55-415f-bcb0-7a457e87c0b4.jpg",
    "https://user-images.githubusercontent.com/81998012/182025215-ce739965-5c4f-4735-8581-566e03b609f2.jpg",    
]

def main():
    google_reverse_image_data = {}

    for index, image_url in enumerate(image_urls, start=1):
        google_reverse_image_data[f"results for image {index}"] = {}

        params = {
            "api_key": "...",                   # https://serpapi.com/manage-api-key
            "engine": "google_reverse_image",   # SerpApi search engine
            "image_url": image_url,             # image URL to perform a reverse search
            "location": "Dallas",               # location from where search comes from
            "hl": "en",                         # language of the search
            "gl": "us"                          # country of the search
            # other parameters
        }
        
        search = GoogleSearch(params)           # where data extraction happens on the SerpApi backend
        results = search.get_dict()             # JSON -> Python dictionary

        # some queries may not include this information
        if results["knowledge_graph"]:
            knowledge_graph = {}
            
            knowledge_graph["title"] = results["knowledge_graph"]["title"]
            knowledge_graph["description"] = results["knowledge_graph"]["description"]
        
            google_reverse_image_data[f"results for image {index}"]["knowledge_graph"] = knowledge_graph

        # some queries may not include organic results
        if results["image_results"]:
            google_reverse_image_data[f"results for image {index}"]["organic_results"] = []
            
            for result in results["image_results"]:
                image_results = {}
                
                image_results["position"] = result["position"]
                image_results["title"] = result["title"]
                image_results["link"] = result["link"]
                image_results["snippet"] = result["snippet"]
        
                google_reverse_image_data[f"results for image {index}"]["organic_results"].append(image_results)

        # some queries may not include this information
        if results["inline_images"]:
            google_reverse_image_data[f"results for image {index}"]["inline_images"] = []
            
            for result in results["inline_images"]:
                google_reverse_image_data[f"results for image {index}"]["inline_images"].append({
                    "source": result["source"],
                    "thumbnail": result["thumbnail"]
                })

    return google_reverse_image_data

if __name__ == "__main__":
    print(json.dumps(main(), indent=4, ensure_ascii=False))

```

### Prerequisites

```lang-none
pip install google-search-results

```

### Code Explanation

Import libraries:

```python
from serpapi import GoogleSearch
import json

```

| Library                                             | Purpose                                                                                        |
| --------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| [json](https://docs.python.org/3/library/json.html) | to convert extracted data to a JSON object.                                                    |
| GoogleSearch                                        | to scrape and parse Google results using [SerpApi](https://serpapi.com/) web scraping library. |

Then we need to have a `list` of URLs to search data from (could be anything that is iterable):

```python
image_urls = [
    "https://user-images.githubusercontent.com/81998012/182214192-59dfb3fe-522c-4979-bb42-9f8091dfd9d6.jpg",
    "https://user-images.githubusercontent.com/81998012/182025185-27df7683-24d5-4747-904b-9f3a6045705b.jpg",
    "https://user-images.githubusercontent.com/81998012/182025195-fec95c5c-aee1-448b-9165-ce9dc1b77a56.jpg",
    "https://user-images.githubusercontent.com/81998012/182027073-4b09a0b7-ec55-415f-bcb0-7a457e87c0b4.jpg",
    "https://user-images.githubusercontent.com/81998012/182025215-ce739965-5c4f-4735-8581-566e03b609f2.jpg",    
]

```

Next, we need to create a `main` function (optional) and create a temporary `dict` to store extracted data:

```python
def main():
    google_reverse_image_data = {}

```

For the next step, we need to iterate over all `image_urls` and pass its value to `"image_url"` `params` key:

```python
for index, image_url in enumerate(image_urls, start=1):
    google_reverse_image_data[f"results for image {index}"] = {}

    params = {
        "engine": "google_reverse_image",   # SerpApi search engine
        "image_url": image_url,             # image URL to perform a reverse search
        "location": "Dallas",               # location from where search comes from
        "hl": "en",                         # language of the search
        "gl": "us",                         # country of the search
        "api_key": "...",                   # https://serpapi.com/manage-api-key
    }
    
    search = GoogleSearch(params)           # where data extraction happens on the SerpApi backend
    results = search.get_dict()

```

| Code                                                                      | Explanation                                                                                                                         |
| ------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| [enumerate()](https://docs.python.org/3/library/functions.html#enumerate) | to add a counter to an iterable and return it. In this case, it's used to show more explicitly which results belong to which image. |

Now we need to check `if` any specific data we want is being returned to us. In this case, we're only checking `knowledge_graph`, `organic_results` (`image_results`), and `inline_images`:

```python
if results["knowledge_graph"]:
    knowledge_graph = {}
    
    knowledge_graph["title"] = results["knowledge_graph"]["title"]
    knowledge_graph["description"] = results["knowledge_graph"]["description"]

    google_reverse_image_data[f"results for image {index}"]["knowledge_graph"] = knowledge_graph

```

```python
if results["image_results"]:
    google_reverse_image_data[f"results for image {index}"]["organic_results"] = []
    
    for result in results["image_results"]:
        image_results = {}
        
        image_results["position"] = result["position"]
        image_results["title"] = result["title"]
        image_results["link"] = result["link"]
        image_results["snippet"] = result["snippet"]

        google_reverse_image_data[f"results for image {index}"]["organic_results"].append(image_results)

```

```python
if results["inline_images"]:
    google_reverse_image_data[f"results for image {index}"]["inline_images"] = []
    
    for result in results["inline_images"]:
        google_reverse_image_data[f"results for image {index}"]["inline_images"].append({
            "source": result["source"],
            "thumbnail": result["thumbnail"]
        })

```

Now we need to return the data:

```python
return google_reverse_image_data

```

The last step would be to add a Python idiom to make sure that [readers understand this code as an executable script](https://www.youtube.com/watch?v=g%5FwlZ9IhbTs) and print the data:

```python
if __name__ == "__main__":
    print(json.dumps(main(), indent=4, ensure_ascii=False))

```

Output:

```json
{
    "results for image 1": {
        "knowledge_graph": {
            "title": "Stairs",
            "description": "Stairs are a structure designed to bridge a large vertical distance by dividing it into smaller vertical distances, called steps. Stairs may be straight, round, or may consist of two or more straight pieces connected at angles. Types of stairs include staircases, ladders, and escalators."
        },
        "organic_results": [
            {
                "position": 1,
                "title": "Stairs - Wikipedia",
                "link": "https://en.wikipedia.org/wiki/Stairs",
                "snippet": "Stairs are a structure designed to bridge a large vertical distance by dividing it into smaller vertical distances, called steps. Stairs may be straight, ..."
            }, ... other organic results
            {
                "position": 4,
                "title": "Foto HD de Claudio Schwarz - Unsplash",
                "link": "https://unsplash.com/es/fotos/ipcsI15th5I",
                "snippet": "Nuevo: Unsplash ahora está disponible en varios idiomas. Puedes volver a cambiar al inglés cuando quieras. Próximamente habrá más idiomas."
            }
        ],
        "inline_images": [
            {
                "source": "https://www.flickr.com/photos/thepiratesgospel/6107309586/",
                "thumbnail": "https://serpapi.com/searches/62e907cb5b54ef5af08d6ff2/images/6886d2b2c5499da05d656e39563b001cc2a1b485150c7a5761ed1190edbccb0f.jpeg"
            }, ... other thumbnails
            {
                "source": "https://en-gb.facebook.com/qualitycarpetsdirect/posts/",
                "thumbnail": "https://serpapi.com/searches/62e907cb5b54ef5af08d6ff2/images/6886d2b2c5499da08e88e75176317a1c08b36d2c2800f2329de12d162eab24e9.jpeg"
            }
        ]
    }, ... other results
    "results for image 5": {
        "knowledge_graph": {
            "title": "Art",
            "description": "Art is a diverse range of human activity, and resulting product, that involves creative or imaginative talent expressive of technical proficiency, beauty, emotional power, or conceptual ideas."
        },
        "organic_results": [
            {
                "position": 1,
                "title": "Art.com | Wall Art: Framed Prints, Canvas Paintings, Posters ...",
                "link": "https://www.art.com/",
                "snippet": "Shop Art.com for the best selection of wall art and photo prints online! Low price guarantee, fast shipping & free returns, and custom framing options ..."
            },
            {
                "position": 2,
                "title": "Art - Wikipedia",
                "link": "https://en.wikipedia.org/wiki/Art",
                "snippet": "Art is a diverse range of human activity, and resulting product, that involves creative or imaginative talent expressive of technical proficiency, beauty, ..."
            }
        ],
        "inline_images": [
            {
                "source": "https://www.leireunzueta.com/journal/tag/summer",
                "thumbnail": "https://serpapi.com/searches/62e907d0e13508b8c60f4c3b/images/6c0c95a05f3c4aa45e83ffe98a6112df67130eb20d484feef2c133f72ab49a3f.jpeg"
            }, ... other thumbnails
            {
                "source": "https://unsplash.com/photos/onMwdrVfMuE",
                "thumbnail": "https://serpapi.com/searches/62e907d0e13508b8c60f4c3b/images/6c0c95a05f3c4aa4deae63325b5a810305c9d32f794fc72c1849753080f116fa.jpeg"
            }
        ]
    }
}

```

## Why using API?

- No need to create a parser from scratch and maintain it.
- Bypass blocks from Google: solve CAPTCHA or solve IP blocks.
- Pay for proxies, and CAPTCHA solvers.
- Don't need to use browser automation.

SerpApi handles everything on the backend with very fast response times and without browser automation, which becomes much faster.

Average is \~2.07 seconds based on 20 search queries (on the screenshot 15 search queries are shown):

![Responce times from Google Reverse Images API under Dashboard, Your Searches page](https://user-images.githubusercontent.com/78694043/182553699-0b0add6d-ae28-4598-9bcc-bc7120dbe78a.png)

## Links

- [Code in the online IDE](https://replit.com/@serpapi/Google-Reverse-Images-API#main.py)
- [Google Reverse Image API](https://serpapi.com/google-reverse-image)

Join us on [Twitter](https://twitter.com/serp%5Fapi) | [YouTube](https://www.youtube.com/channel/UCUgIHlYBOD3yA3yDIRhg%5Fmg)

Add a [Feature Request](https://github.com/serpapi/public-roadmap/issues)💫 or a [Bug](https://github.com/serpapi/public-roadmap/issues)🐞