comic-hasher/cmd/hash.py

57 lines
1.3 KiB
Python
Raw Normal View History

2024-05-10 13:58:04 -07:00
from typing import Collection, Sequence
from PIL import Image
import argparse,pathlib,numpy,imagehash
ap = argparse.ArgumentParser()
ap.add_argument("--file", type=pathlib.Path)
opts = ap.parse_args()
opts.file = pathlib.Path(opts.file)
hash_size = 8
image = Image.open(opts.file)
gray = image.copy().convert('L')
resized = gray.copy().resize((hash_size, hash_size), Image.Resampling.LANCZOS)
def print_image(image: Image.Image) -> None:
for row in numpy.asarray(image):
print('[ ', end='')
for i in row:
if isinstance(i, Collection):
print('{ ', end='')
for idx, x in enumerate(i):
if idx == len(i)-1:
print(f'{int(x):03d} ', end='')
else:
print(f'{int(x):03d}, ', end='')
print('}, ', end='')
else:
print(f'{int(i):03d}, ', end='')
print(']')
def bin_str(hash):
return ''.join(str(b) for b in 1 * hash.hash.flatten())
2024-07-31 11:35:17 -07:00
# print("rgb")
# print_image(image)
# print()
2024-05-10 13:58:04 -07:00
image.save("py.rgb.png")
2024-07-31 11:35:17 -07:00
# print("gray")
# print_image(gray)
2024-05-10 13:58:04 -07:00
gray.save("py.gray.png")
2024-07-31 11:35:17 -07:00
# print()
2024-05-10 13:58:04 -07:00
2024-07-31 11:35:17 -07:00
# print("resized")
# print_image(resized)
2024-05-10 13:58:04 -07:00
resized.save("py.resized.png")
2024-07-31 11:35:17 -07:00
# print()
2024-05-10 13:58:04 -07:00
2024-07-31 11:35:17 -07:00
print(str(imagehash.average_hash(image)))
print(str(imagehash.dhash(image)))
print(str(imagehash.phash(image)))