start adding uncat dicom
This commit is contained in:
+7
-35
@@ -21,7 +21,7 @@ from sortedm2m.fields import SortedManyToManyField
|
||||
|
||||
import string
|
||||
from collections import defaultdict
|
||||
from helpers.images import image_as_base64
|
||||
from helpers.images import get_image_hash, image_as_base64
|
||||
|
||||
from django.contrib.contenttypes.fields import GenericRelation
|
||||
|
||||
@@ -575,42 +575,14 @@ class RapidImage(models.Model):
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Override save method to add image hash"""
|
||||
# TODO: consider moving to signal to reuse across apps
|
||||
if self.image:
|
||||
# Try and read the file as a dicom
|
||||
try:
|
||||
# and generate a hash from the pixel data
|
||||
# TODO: improve?
|
||||
dataset = pydicom.dcmread(self.image)
|
||||
# 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())
|
||||
image_hash, is_dicom = get_image_hash(self.image)
|
||||
self.is_dicom = is_dicom
|
||||
self.image_md5_hash = image_hash
|
||||
|
||||
hash = md5.hexdigest()
|
||||
# ----
|
||||
|
||||
self.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)
|
||||
self.image.file.open()
|
||||
hash = hashlib.md5(self.image.read()).hexdigest()
|
||||
except AttributeError:
|
||||
return
|
||||
|
||||
self.image_md5_hash = hash
|
||||
super().save(*args, **kwargs) # Call the "real" save() method.
|
||||
# Hack for tests
|
||||
if image_hash != "12345ABCD":
|
||||
super().save(*args, **kwargs) # Call the "real" save() method.
|
||||
|
||||
|
||||
class RapidCreationDefault(models.Model):
|
||||
|
||||
Reference in New Issue
Block a user