Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageDraw
- from PIL.JpegImagePlugin import JpegImageFile
- def create_photo_bits(photo_name: str):
- old_photo: JpegImageFile = Image.open(photo_name)
- new_photo = old_photo.resize((16, 16))
- draw = ImageDraw.Draw(new_photo)
- pix = new_photo.load()
- for i in range(16):
- for j in range(16):
- a = pix[i, j][0]
- b = pix[i, j][1]
- c = pix[i, j][2]
- S = a + b + c
- if S > ((255 // 2) * 3):
- a, b, c = 255, 255, 255
- else:
- a, b, c = 0, 0, 0
- draw.point((i, j), (a, b, c))
- bits = ""
- for i in range(16):
- for j in range(16):
- for pix_ in pix[i, j]:
- if pix_ == 255:
- bits += "1"
- else:
- bits += "0"
- return hex(int(bits))
- def eq(original: str, eq_photo: str):
- original = create_photo_bits(original)
- hex_bits = create_photo_bits(eq_photo)
- count_eq = 0
- min_len = min([len(hex_bits), len(original)])
- original = str(original)[:min_len]
- hex_bits = str(hex_bits)[:min_len]
- for num, letter in enumerate(original):
- if letter == hex_bits[num]:
- count_eq += 1
- return round(100 * (count_eq / min_len), 2)
- print(eq("test_images/test.jpg", "test_images/test-min.jpg"))
- print(eq("test_images/test.jpg", "test_images/test-resized.jpg"))
- print(eq("test_images/test.jpg", "test_images/test-min-resized.jpg"))
- print(eq("test_images/test.jpg", "test_images/other.jpg"))
- print(eq("test_images/test.jpg", "test_images/other2.jpg"))
- print(eq("test_images/test.jpg", "test_images/other3.jpg"))
- print(eq("test_images/test.jpg", "test_images/other4.jpg"))
- print(eq("test_images/test.jpg", "test_images/other5.jpg"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement