Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. import sys
  2. import os
  3. import numpy as np
  4.  
  5. from PIL import Image
  6. from matplotlib import pyplot as plt
  7. from scipy import spatial
  8.  
  9.  
  10. def sortImages(inputDir, image, OutputDir):
  11.     image = Image.open(image)
  12.  
  13.     if not os.path.exists(OutputDir):
  14.         os.makedirs(OutputDir)
  15.  
  16.     if image.mode not in  {"RGB, RGBA"}:
  17.         image = image.convert("RGB")
  18.  
  19.     image = np.array(image)
  20.  
  21.     plt.hist(image.flatten(), bins=128)
  22.     plt.savefig("image.png")
  23.     plt.clf()
  24.  
  25.     image = Image.open("image.png")
  26.     image = np.array(image)
  27.     image = image.flatten()
  28.  
  29.     values = dict()
  30.     for file in os.listdir(inputDir):
  31.         comparison = Image.open(inputDir + file)
  32.  
  33.         print(file)
  34.         if comparison.mode not in  {"RGB, RGBA"}:
  35.             comparison = comparison.convert("RGB")
  36.  
  37.         comparison = np.array(comparison)
  38.  
  39.         plt.hist(comparison.flatten(), bins=128)
  40.         plt.savefig("comparison.png")
  41.         plt.clf()
  42.  
  43.         comparison = Image.open("comparison.png")
  44.         comparison = np.array(comparison)
  45.         comparison = comparison.flatten()
  46.  
  47.         value = abs(spatial.distance.cosine(image, comparison))
  48.         values[file] = format(value, ".20f")
  49.  
  50.     chosen = dict()
  51.     for keys, value in values.items():
  52.         if float(value) < 0.2:
  53.             chosen[keys] = value
  54.  
  55.     chosen = sorted(chosen, key=chosen.get)
  56.  
  57.     for image in chosen:
  58.         Image.open(inputDir + image).save(OutputDir + image)
  59.  
  60. sortImages(sys.argv[1], sys.argv[2], sys.argv[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement