5 min read

Deduplicate Google Images Results Without Losing Source Pages

Group Google Images results by original URL in Python while keeping every source page, query, position, and thumbnail.

• 850 words

A seen_urls set does a fine job of keeping duplicate tiles out of an image grid. Use that same filter to clean your source data, though, and the second result disappears with its page URL and position. If the image is part of a research log, that second result might be the one you need to revisit.

Keep one asset per exact original URL. Under that asset, keep one observation per result row, with the query, position, source page and thumbnail. The grid can display the asset once while the log still shows where it turned up.

What disappears when you keep only the first result?

On December 5, 2026, we ran two US English searches with the SearchCans Google Images API. We saved data.images_results for NASA blue marble 1972 Earth and Apollo 17 blue marble NASA photo and grouped the returned rows by the exact original value. We used the returned URLs as data and left the image files untouched.

Count In these two responses
Result rows 198
Distinct original URLs 157
Original URLs reported more than once 41
Original URLs with more than one distinct source page URL 5

One image URL on npr.brightspotcdn.com appeared at position 18 with a page on www.wmra.org and at position 94 in the second search with a page on www.wamc.org. A flat deduplicated export would keep one of those page links. An asset with two observations keeps both, with enough context to tell what each position means.

Give the image and each result their own records

Use original as the asset key and link as the page URL returned for that observation. Keep thumbnail on the observation so the result has its original preview. When original is missing or unusable, hold that row separately. Joining on the thumbnail would let a preview decide which original file a result refers to.

Keep the original URL string intact, including its query parameters. If two URLs differ only in a transform or signature, this script makes two asset records. Checking whether both URLs serve identical bytes is a separate step that requires fetching the files. The source page records where the result was reported. Check the page itself before relying on it for attribution or reuse.

Group saved responses with Python

Save each response with its request context in one JSON array. Use a distinct capture_id per request. Here is a one-row example with made-up URLs. Put the full API response JSON under response for each of your captures. If you need to make the requests first, follow the Python guide to getting Google Images results as JSON.

[
  {
    "capture_id": "run-a",
    "query": "blue marble",
    "country": "us",
    "language": "en",
    "response": {
      "code": 0,
      "data": {
        "images_results": [
          {
            "original": "https://img.example.org/earth.jpg",
            "link": "https://archive.example.org/earth",
            "thumbnail": "https://img.example.org/earth-small.jpg",
            "position": 1
          }
        ]
      }
    }
  }
]

Save the code below as group_image_results.py and run python group_image_results.py captures.json. The script reads local JSON and prints the grouped output.

import json
from pathlib import Path
import sys
from urllib.parse import urlsplit


def original_url(value):
    if not isinstance(value, str) or any(c.isspace() or ord(c) < 32 for c in value):
        return None
    try:
        url = urlsplit(value)
        if url.scheme in {"http", "https"} and url.hostname and url.username is None:
            return value
    except ValueError:
        pass
    return None


def group_captures(captures):
    assets = {}
    unresolved = []
    failed = []

    for capture in captures:
        context = {key: capture[key] for key in ("capture_id", "query", "country", "language")}
        response = capture.get("response")
        code = response.get("code") if isinstance(response, dict) else None
        data = response.get("data") if isinstance(response, dict) else None
        rows = data.get("images_results") if isinstance(data, dict) else None
        if type(code) is not int or code != 0 or not isinstance(rows, list):
            failed.append({**context, "api_code": code, "reason": "invalid image response"})
            continue

        for row_index, result in enumerate(rows):
            record = {**context, "row_index": row_index}
            if not isinstance(result, dict):
                unresolved.append({**record, "reason": "invalid result row", "raw_row": result})
                continue
            observation = {
                **record,
                "position": result.get("position"),
                "source_page": result.get("link"),
                "thumbnail": result.get("thumbnail"),
            }
            original = result.get("original")
            key = original_url(original)
            if key is None:
                unresolved.append({**observation, "reported_original": original})
                continue

            asset = assets.setdefault(key, {"original": key, "observations": []})
            asset["observations"].append(observation)

    return {"assets": list(assets.values()), "unresolved": unresolved, "failed": failed}


if __name__ == "__main__":
    captures = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
    print(json.dumps(group_captures(captures), indent=2))

Check that the source records survived

In the example input, copy the result row and change only its link. The output should contain one asset with two observations. Now give the copied row a different original and leave its thumbnail alone. You should get two assets. If you remove the copied row’s original, it should move to unresolved with its source and position intact.

For each successful capture, the row count should equal its grouped observations plus its unresolved rows. A response with code: 0 and images_results: [] contributes zero rows. A failed API response goes into failed, so a collection job can tell which request needs another look.

SearchCans Team

SearchCans Team

SERP API & Reader API Experts

The SearchCans engineering team builds high-performance search APIs serving developers worldwide. We share practical tutorials, best practices, and insights on SERP data, web scraping, RAG pipelines, and AI integration.

Ready to build with SearchCans?

Test SERP API and Reader API with 100 free credits. No credit card required.