start adding uncat dicom
This commit is contained in:
+38
-1
@@ -1,4 +1,5 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import mimetypes
|
||||
|
||||
from io import BytesIO
|
||||
@@ -152,4 +153,40 @@ def print_dicom(dataset, indent=0):
|
||||
l.append("{0:s} {1:s} = {2:s}".format(indent_string,
|
||||
data_element.name,
|
||||
repr_value))
|
||||
return "\n<br/>".join(l)
|
||||
return "\n<br/>".join(l)
|
||||
|
||||
def get_image_hash(img) -> (str, bool):
|
||||
is_dicom = False
|
||||
# Try and read the file as a dicom
|
||||
try:
|
||||
# and generate a hash from the pixel data
|
||||
# TODO: improve?
|
||||
dataset = pydicom.dcmread(img)
|
||||
# flatten = dataset.pixel_array.astype(str).flatten()
|
||||
# print("flatteded")
|
||||
# pre_join = ",".join(flatten)
|
||||
# print(pre_join)
|
||||
# hash = hashlib.md5(pre_join.encode()).hexdigest()
|
||||
# ----
|
||||
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()
|
||||
is_dicom = True
|
||||
# ----
|
||||
|
||||
except pydicom.errors.InvalidDicomError:
|
||||
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, is_dicom
|
||||
Reference in New Issue
Block a user