Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- import numpy as np
- from PIL import Image
- from matplotlib import pyplot as plt
- from scipy import spatial
- def sortImages(inputDir, image, OutputDir):
- image = Image.open(image)
- if not os.path.exists(OutputDir):
- os.makedirs(OutputDir)
- if image.mode not in {"RGB, RGBA"}:
- image = image.convert("RGB")
- image = np.array(image)
- plt.hist(image.flatten(), bins=128)
- plt.savefig("image.png")
- plt.clf()
- image = Image.open("image.png")
- image = np.array(image)
- image = image.flatten()
- values = dict()
- for file in os.listdir(inputDir):
- comparison = Image.open(inputDir + file)
- print(file)
- if comparison.mode not in {"RGB, RGBA"}:
- comparison = comparison.convert("RGB")
- comparison = np.array(comparison)
- plt.hist(comparison.flatten(), bins=128)
- plt.savefig("comparison.png")
- plt.clf()
- comparison = Image.open("comparison.png")
- comparison = np.array(comparison)
- comparison = comparison.flatten()
- value = abs(spatial.distance.cosine(image, comparison))
- values[file] = format(value, ".20f")
- chosen = dict()
- for keys, value in values.items():
- if float(value) < 0.2:
- chosen[keys] = value
- chosen = sorted(chosen, key=chosen.get)
- for image in chosen:
- Image.open(inputDir + image).save(OutputDir + image)
- sortImages(sys.argv[1], sys.argv[2], sys.argv[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement