fix image hashes (possible)

This commit is contained in:
Ross
2023-08-21 14:05:54 +01:00
parent fcd77514eb
commit 48cba9353e
6 changed files with 119 additions and 42 deletions
+17 -2
View File
@@ -155,9 +155,24 @@ def print_dicom(dataset, indent=0):
repr_value))
return "\n<br/>".join(l)
def get_image_dicom_hash(img) -> (str, bool):
dataset = pydicom.dcmread(img)
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())
hash = md5.hexdigest()
return hash
def get_image_hash(img) -> (str, bool):
is_dicom = False
# Try and read the file as a dicom
hash = "1234"
try:
# and generate a hash from the pixel data
# TODO: improve?
@@ -181,12 +196,12 @@ def get_image_hash(img) -> (str, bool):
is_dicom = True
# ----
except pydicom.errors.InvalidDicomError:
except pydicom.errors.InvalidDicomError as e:
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()
except AttributeError:
return "12345ABCD", False
return hash, False
return hash, is_dicom