Advertisement
kesha1225

Untitled

Apr 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. from PIL import Image, ImageDraw
  2. from PIL.JpegImagePlugin import JpegImageFile
  3.  
  4.  
  5. def create_photo_bits(photo_name: str):
  6. old_photo: JpegImageFile = Image.open(photo_name)
  7.  
  8. new_photo = old_photo.resize((16, 16))
  9. draw = ImageDraw.Draw(new_photo)
  10.  
  11. pix = new_photo.load()
  12.  
  13. for i in range(16):
  14. for j in range(16):
  15. a = pix[i, j][0]
  16. b = pix[i, j][1]
  17. c = pix[i, j][2]
  18. S = a + b + c
  19. if S > ((255 // 2) * 3):
  20. a, b, c = 255, 255, 255
  21. else:
  22. a, b, c = 0, 0, 0
  23. draw.point((i, j), (a, b, c))
  24.  
  25. bits = ""
  26. for i in range(16):
  27. for j in range(16):
  28. for pix_ in pix[i, j]:
  29. if pix_ == 255:
  30. bits += "1"
  31. else:
  32. bits += "0"
  33. return hex(int(bits))
  34.  
  35.  
  36. def eq(original: str, eq_photo: str):
  37. original = create_photo_bits(original)
  38. hex_bits = create_photo_bits(eq_photo)
  39.  
  40. count_eq = 0
  41.  
  42. min_len = min([len(hex_bits), len(original)])
  43. original = str(original)[:min_len]
  44. hex_bits = str(hex_bits)[:min_len]
  45.  
  46. for num, letter in enumerate(original):
  47. if letter == hex_bits[num]:
  48. count_eq += 1
  49.  
  50. return round(100 * (count_eq / min_len), 2)
  51.  
  52.  
  53. print(eq("test_images/test.jpg", "test_images/test-min.jpg"))
  54. print(eq("test_images/test.jpg", "test_images/test-resized.jpg"))
  55. print(eq("test_images/test.jpg", "test_images/test-min-resized.jpg"))
  56. print(eq("test_images/test.jpg", "test_images/other.jpg"))
  57. print(eq("test_images/test.jpg", "test_images/other2.jpg"))
  58. print(eq("test_images/test.jpg", "test_images/other3.jpg"))
  59. print(eq("test_images/test.jpg", "test_images/other4.jpg"))
  60. print(eq("test_images/test.jpg", "test_images/other5.jpg"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement