improve image hashing

This commit is contained in:
Ross
2023-12-26 17:11:02 +00:00
parent 8c0aaa82e2
commit 714f472da6
14 changed files with 183 additions and 42 deletions
+29 -13
View File
@@ -5,6 +5,7 @@ import mimetypes
from io import BytesIO
from PIL import Image, ImageFile
from blake3 import blake3
import pydicom
import sys
@@ -153,38 +154,53 @@ def print_dicom(dataset, indent=0, include_tag_ids=True):
l.append(f"{data_element.tag}: {indent_string:s} {data_element.name:s} = {repr_value:s}")
return "\n<br/>".join(l)
def get_image_dicom_hash(img, dataset=None) -> (str, bool):
def get_image_dicom_hash(img, dataset=None, hash_type="md5", direct_pixel_data=True) -> (str, bool):
if dataset is None:
dataset = pydicom.dcmread(img)
# TODO: improve?
md5 = hashlib.md5()
first = True
for i in dataset.pixel_array.astype(str).flatten():
if first:
first = False
md5.update(f"{i}".encode())
else:
md5.update(f",{i}".encode())
match hash_type:
case "md5":
hasher = hashlib.md5()
case "blake3":
hasher = blake3()
case _:
raise NotImplemented
hash = md5.hexdigest()
if direct_pixel_data:
hasher.update(dataset.PixelData)
else:
first = True
for i in dataset.pixel_array.astype(str).flatten():
if first:
first = False
hasher.update(f"{i}".encode())
else:
hasher.update(f",{i}".encode())
hash = hasher.hexdigest()
return hash
def get_image_hash(img) -> (str, bool):
def get_image_hash(img, hash_type="blake3", direct_pixel_data=True) -> (str, bool):
is_dicom = False
# Try and read the file as a dicom
hash = "1234"
try:
# and generate a hash from the pixel data
hash = get_image_dicom_hash(img)
hash = get_image_dicom_hash(img, hash_type=hash_type, direct_pixel_data=direct_pixel_data)
is_dicom = True
# ----
except pydicom.errors.InvalidDicomError as e:
print("dicom error")
try: # This is horrible (but needed for current unit tests)
# (we use a temporary file that breaks here)
img.file.open()
hash = hashlib.md5(img.read()).hexdigest()
match hash_type:
case "md5":
hash = hashlib.md5(img.read()).hexdigest()
case "blake3":
hash = blake3(img.read()).hexdigest()
except AttributeError:
return hash, False