Skip to content

Commit

Permalink
DICOM redactor improvement: Enabling more photometric interpretations (
Browse files Browse the repository at this point in the history
…#1103)

* Add improved check for greyscale

* Using uuid for temp file name rather than hard coded single string
  • Loading branch information
niwilso authored Jul 6, 2023
1 parent a1c8a23 commit 42f30bd
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
import shutil
from copy import deepcopy
import tempfile
Expand Down Expand Up @@ -58,9 +59,10 @@ def redact(
# Convert DICOM to PNG and add padding for OCR (during analysis)
is_greyscale = self._check_if_greyscale(instance)
image = self._rescale_dcm_pixel_array(instance, is_greyscale)
self._save_pixel_array_as_png(image, is_greyscale, "tmp_dcm", tmpdirname)
image_name = str(uuid.uuid4())
self._save_pixel_array_as_png(image, is_greyscale, image_name, tmpdirname)

png_filepath = f"{tmpdirname}/tmp_dcm.png"
png_filepath = f"{tmpdirname}/{image_name}.png"
loaded_image = Image.open(png_filepath)
image = self._add_padding(loaded_image, is_greyscale, padding_width)

Expand Down Expand Up @@ -225,8 +227,11 @@ def _check_if_greyscale(instance: pydicom.dataset.FileDataset) -> bool:
:return: FALSE if the Photometric Interpretation is RGB.
"""
# Check if image is grayscale using the Photometric Interpretation element
color_scale = instance[0x0028, 0x0004].value
is_greyscale = color_scale != "RGB"
try:
color_scale = instance.PhotometricInterpretation
except AttributeError:
color_scale = None
is_greyscale = (color_scale in ["MONOCHROME1", "MONOCHROME2"])

return is_greyscale

Expand All @@ -243,7 +248,10 @@ def _rescale_dcm_pixel_array(
"""
# Normalize contrast
if "WindowWidth" in instance:
image_2d = apply_voi_lut(instance.pixel_array, instance)
if is_greyscale:
image_2d = apply_voi_lut(instance.pixel_array, instance)
else:
image_2d = instance.pixel_array
else:
image_2d = instance.pixel_array

Expand Down

0 comments on commit 42f30bd

Please sign in to comment.