Reverse image search

Reverse image search is a search for images that are similar to a given reference image.

In the GroupDocs.Search index, reverse image search allows you to search for similar images in ZIP archives, various documents, and individual files. The image search is performed by comparing the perceptual hash of the reference image with the hashes of the images in the index. The idea of a perceptual hash is that for very similar images it has a value with a minimum number of different bits, and for very different images it gives a large number of different bits.

In the GroupDocs.Search engine, reverse image search, like full-text search, consists of two stages: the indexing stage and the actual search stage.

To search for images in the index, you must enable at least one of the flags in the options at the indexing stage:

  • enabled_for_separate_images is for indexing images in separate files.
  • enabled_for_embedded_images is for indexing images embedded in various documents.
  • enabled_for_container_item_images is for indexing images that are elements of container documents, such as ZIP archives, OST/PST storages. For more information, see the Indexing options page.

The following options are available during the search stage:

  • hash_differences sets the maximum number of different bits in the hashes of found images. The hash is 64 bits long, so this value ranges from 0 to 64.
  • max_result_count sets the maximum number of found images.
  • search_document_filter sets the filter for found documents. For more information, see the Image search options page.

A reference image is created with one of the overloads of the SearchImage.create method:

  • create(file_path) creates a reference image from an image file.
  • create(stream) creates a reference image from a stream containing an image, for example, a binary file object.
  • create(file_path, image_index) and create(stream, image_index) create a reference image from the specified frame (zero-based index) of a multipage image, for example a multipage TIFF image.

The search result is returned as an instance of the ImageSearchResult class. The image_count property returns the number of found images, and the get_found_image method returns a found image by its index. The found images with fewer different bits in the hash are returned first. Each found image is represented by an instance of the FoundImageFrame class, which has the following properties:

  • document_info returns information about the document containing the found image.
  • image_index returns the ordinal number of the image in the document.
  • frame_index returns the index of the frame in the image.
  • hash_differences returns the number of different bits in the hash of the found image.

The following code example demonstrates all stages of the reverse image search:

import os

from groupdocs.search import Index
from groupdocs.search.common import SearchImage
from groupdocs.search.options import ImageSearchOptions, IndexingOptions, SearchDocumentFilter

# Creating an index
index = Index("./index")

# Setting the image indexing options
indexing_options = IndexingOptions()
indexing_options.image_indexing_options.enabled_for_container_item_images = True
indexing_options.image_indexing_options.enabled_for_embedded_images = True
indexing_options.image_indexing_options.enabled_for_separate_images = True

# Indexing documents in a document folder
index.add("./documents", indexing_options)

# Setting the image search options
image_search_options = ImageSearchOptions()
image_search_options.hash_differences = 10
image_search_options.max_result_count = 100
image_search_options.search_document_filter = SearchDocumentFilter.create_file_extension(".zip", ".png", ".jpg")

# Creating a reference image for search
search_image = SearchImage.create("./image.png")

# Searching in the index
result = index.search(search_image, image_search_options)

print("Images found: " + str(result.image_count))
for i in range(result.image_count):
    image = result.get_found_image(i)
    document_info = image.document_info
    name = os.path.basename(document_info.file_path)
    if document_info.inner_path:
        name += " / " + document_info.inner_path  # An item of a container document, such as a ZIP archive
    print(name + ", image " + str(image.image_index) + ", frame " + str(image.frame_index) + ", different bits: " + str(image.hash_differences))

The following sample file is used in this example: image.png

Images found: 3
holiday-photos.zip / photos\sunset.png, image 0, frame 0, different bits: 0
sunset.png, image 0, frame 0, different bits: 0
sunset-brighter.png, image 0, frame 0, different bits: 4

Download full output

More resources

GitHub examples

You may easily run the code from documentation articles and see the features in action in our GitHub examples:

Free online document search App

Along with the full-featured library, we provide simple but powerful free apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.