Skip to content

Instantly share code, notes, and snippets.

@johnbumgarner
Last active January 2, 2021 13:20

Revisions

  1. johnbumgarner revised this gist Jan 2, 2021. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions comparison_scores_skimage.py
    Original file line number Diff line number Diff line change
    @@ -45,9 +45,11 @@ def determine_image_similarities(base_image, comparison_image):
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as thread_pool_executor:
    for (directory_path, directory_names, file_names) in walk(image_folder):
    for file_name in file_names:
    compare_image = resize(imread(path.join(dirpath, filename), as_gray=True), (128, 128), preserve_range=True, mode='constant')
    args = (ref_image, compare_image)
    future = [thread_pool_executor.submit(lambda p: determine_image_similarities(*p), args)]
    for hash_to_compare in concurrent.futures.as_completed(future):
    print(hash_to_compare)
    accepted_extensions = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.svg', '.tiff')
    if file_name.endswith(accepted_extensions):
    compare_image = resize(imread(path.join(directory_path, file_name), as_gray=True), (128, 128), preserve_range=True, mode='constant')
    args = (ref_image, compare_image)
    future = [thread_pool_executor.submit(lambda p: determine_image_similarities(*p), args)]
    for hash_to_compare in concurrent.futures.as_completed(future):
    print(hash_to_compare)

  2. johnbumgarner revised this gist Jan 2, 2021. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion comparison_scores_skimage.py
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,20 @@
    #############################################################################################
    # The OS module in provides functions for interacting with the operating system.
    #
    # os.path()
    # os.path() provides various functions to handle pathnames.
    # os.walk() generate the file names in a directory tree by walking the tree.
    #############################################################################################
    from os import path, walk

    #############################################################################################
    # The scikit-image module has a collection of algorithms for image processing.
    #
    # skimage.io.imread - load an image from a file.
    #
    # skimage.transform.resize - resizes an image to match a certain size.
    #
    # skimage.metrics.structural_similarity - compute the mean structural similarity index
    # between two images.
    #############################################################################################
    from skimage.io import imread
    from skimage.transform import resize
    @@ -32,6 +39,7 @@ def determine_image_similarities(base_image, comparison_image):

    image_folder = 'image_directory'

    # https://scikit-image.org/docs/dev/api/skimage.transform.html?highlight=skimage%20transform%20resize#skimage.transform.resize
    ref_image = resize(imread('base_image_name.jpeg', as_gray=True), (128, 128), preserve_range=True, mode='constant')

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as thread_pool_executor:
  3. johnbumgarner created this gist Jan 2, 2021.
    45 changes: 45 additions & 0 deletions comparison_scores_skimage.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    ######################################################################################
    # The concurrent.futures module is part of the standard Python library which provides
    # a high level API for launching asynchronous tasks.
    ######################################################################################
    import concurrent.futures

    #############################################################################################
    # The OS module in provides functions for interacting with the operating system.
    #
    # os.path()
    # os.walk() generate the file names in a directory tree by walking the tree.
    #############################################################################################
    from os import path, walk

    #############################################################################################
    #
    #############################################################################################
    from skimage.io import imread
    from skimage.transform import resize
    from skimage.metrics import structural_similarity as ssim


    def determine_image_similarities(base_image, comparison_image):
    """
    Determine the computational score associated with two different images
    :param base_image:
    :param comparison_image:
    :return: comparison score
    """
    computational_score = ssim(base_image, comparison_image)
    return computational_score

    image_folder = 'image_directory'

    ref_image = resize(imread('base_image_name.jpeg', as_gray=True), (128, 128), preserve_range=True, mode='constant')

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as thread_pool_executor:
    for (directory_path, directory_names, file_names) in walk(image_folder):
    for file_name in file_names:
    compare_image = resize(imread(path.join(dirpath, filename), as_gray=True), (128, 128), preserve_range=True, mode='constant')
    args = (ref_image, compare_image)
    future = [thread_pool_executor.submit(lambda p: determine_image_similarities(*p), args)]
    for hash_to_compare in concurrent.futures.as_completed(future):
    print(hash_to_compare)