Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. from PIL import Image
  2. from PIL import ImageChops
  3. from os import listdir
  4. from os.path import isfile, join
  5. import numpy as np
  6. import sys
  7.  
  8. def pixel_diff(image_a, image_b):
  9. diff = ImageChops.difference(image_a.convert('L'), image_b.convert('L'))
  10. diff = diff.convert('L')
  11.  
  12. return diff
  13.  
  14.  
  15. def total_histogram_diff(pixel_diff):
  16. return sum(i * n for i, n in enumerate(pixel_diff.histogram()))
  17.  
  18. def image_diff(image_a, image_b):
  19. if isinstance(image_a, str):
  20. image_a = Image.open(image_a)
  21.  
  22. if isinstance(image_b, str):
  23. image_b = Image.open(image_b)
  24. histogram_diff = total_histogram_diff(pixel_diff(image_a, image_b))
  25. return histogram_diff
  26.  
  27. def is_equal(image_a, image_b, tolerance=0.0):
  28. return image_diff_percent(image_a, image_b) <= tolerance
  29.  
  30. def image_diff_percent(image_a, image_b):
  31.  
  32. if isinstance(image_a, str):
  33. image_a = Image.open(image_a)
  34.  
  35. if isinstance(image_b, str):
  36. image_b = Image.open(image_b)
  37.  
  38. input_images_histogram_diff = image_diff(image_a, image_b)
  39.  
  40. black_reference_image = Image.new('RGB', image_a.size, (0, 0, 0))
  41. white_reference_image = Image.new('RGB', image_a.size, (255, 255, 255))
  42.  
  43. worst_bw_diff = image_diff(black_reference_image, white_reference_image)
  44.  
  45. percentage_histogram_diff = (input_images_histogram_diff / float(worst_bw_diff)) * 100
  46.  
  47. return percentage_histogram_diff
  48.  
  49.  
  50. def hash_to_string(x):
  51. ret = ''
  52. for i in x.reshape(-1):
  53. if i == True:
  54. ret+='1'
  55. else:
  56. ret+='0'
  57. return ret
  58. def diff(image_a, image_b):
  59. res = 0
  60. if isinstance(image_a, str):
  61. image_a = Image.open(image_a)
  62. if isinstance(image_b, str):
  63. image_b = Image.open(image_b)
  64.  
  65. a_hash = average_hash(image_a)
  66. b_hash = average_hash(image_b)
  67.  
  68. for i in range(len(a_hash)):
  69. if(a_hash[i] != b_hash[i]):
  70. res +=1
  71. return res
  72. def average_hash(image, hash_size=8):
  73. image = image.convert("RGB").resize((hash_size, hash_size), Image.ANTIALIAS)
  74. pixels = np.asarray(image)
  75. r = np.zeros(shape = (hash_size, hash_size), dtype = float)
  76. g = np.copy(r)
  77. b = np.copy(r)
  78. for i,j in np.ndindex(r.shape):
  79. r[i][j] = pixels[i][j][0]
  80. g[i][j] = pixels[i][j][1]
  81. b[i][j] = pixels[i][j][2]
  82. avg_r = r.mean()
  83. avg_g = g.mean()
  84. avg_b = b.mean()
  85. diff_r = r > avg_r
  86. diff_g = g > avg_b
  87. diff_b = b > avg_b
  88. return hash_to_string(diff_r) + hash_to_string(diff_g) + hash_to_string(diff_b)
  89.  
  90. if ("--path" not in sys.argv):
  91. print("Arguments:")
  92. print("")
  93. print("\t\t--path PATH\t specifies path to images")
  94. print("\t\t--help, -h \t get some help")
  95. print("\n")
  96. exit()
  97.  
  98. pathIndex = sys.argv.index("--path") + 1
  99. if ((pathIndex != len(sys.argv)) & (len(sys.argv) > 2)):
  100. pathToImages = sys.argv[pathIndex] + "\\"
  101. else:
  102. exit()
  103.  
  104. imagesNames = [f for f in listdir(pathToImages) if isfile(join(pathToImages, f))]
  105.  
  106. images = []
  107. for imageName in imagesNames:
  108. images.append(Image.open(pathToImages + imageName))
  109.  
  110. results = []
  111. for i in range(len(images)):
  112. for j in range(i + 1, len(images)):
  113. if (is_equal(images[i], images[j])):
  114. results.append([i, j])
  115. elif (diff(images[i], images[j]) <=10 ):
  116. results.append([i,j])
  117.  
  118.  
  119. for result in results:
  120. print(imagesNames[result[0]] + " " + imagesNames[result[1]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement