Last active
January 2, 2021 13:20
This function is designed to generate comparison scores between two image using ssim from skimage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###################################################################################### | |
# 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() 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 | |
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' | |
# 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: | |
for (directory_path, directory_names, file_names) in walk(image_folder): | |
for file_name in file_names: | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment