diff --git a/helpers/__init__.py b/helpers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/helpers/images.py b/helpers/images.py index 7ef67192..453906c7 100644 --- a/helpers/images.py +++ b/helpers/images.py @@ -6,7 +6,11 @@ from io import BytesIO from PIL import Image, ImageFile import pydicom -from .pydicom_PIL import get_PIL_image + +try: + from .pydicom_PIL import get_PIL_image +except: + from pydicom_PIL import get_PIL_image def image_as_base64(image_file): """ @@ -42,11 +46,11 @@ def pil_dicom_image(source, exif_orientation=True, **options): # return #source = BytesIO(source.read()) - print("start pdi") - + # open file with pydicom ds = pydicom.read_file(source) - image = get_PIL_image(ds) + # return the image file + return get_PIL_image(ds) #with Image.open(source) as image: # # Fully load the image now to catch any problems with the image contents. @@ -58,4 +62,10 @@ def pil_dicom_image(source, exif_orientation=True, **options): #if exif_orientation: # image = utils.exif_orientation(image) - return image + + + +def show_PIL(dataset): + """Display an image using the Python Imaging Library (PIL)""" + im = get_PIL_image(dataset) + im.show() \ No newline at end of file diff --git a/helpers/pydicom_PIL.py b/helpers/pydicom_PIL.py index 6b5fcbf2..ebf1b82a 100644 --- a/helpers/pydicom_PIL.py +++ b/helpers/pydicom_PIL.py @@ -23,7 +23,10 @@ and Python Imaging Library: have_PIL = True try: - import PIL.Image + import PIL.Image, PIL.ImageOps + + from pydicom.pixel_data_handlers.util import apply_voi_lut, apply_modality_lut + except ImportError: have_PIL = False @@ -59,6 +62,13 @@ def get_PIL_image(dataset): if ('PixelData' not in dataset): raise TypeError("Cannot show image -- DICOM dataset does not have " "pixel data") + + + # Apply modality lut + array = apply_modality_lut(dataset.pixel_array, dataset) + # voi lut doesn't seem to help + #array = apply_voi_lut(array, dataset) + # can only apply LUT if these window info exists if ('WindowWidth' not in dataset) or ('WindowCenter' not in dataset): bits = dataset.BitsAllocated @@ -79,19 +89,28 @@ def get_PIL_image(dataset): # PIL size = (width, height) size = (dataset.Columns, dataset.Rows) + array = apply_modality_lut(dataset.pixel_array, dataset) + + array = apply_voi_lut(array, dataset) + + if dataset["PhotometricInterpretation"].value == "MONOCHROME1": + # We need to invert + array = np.invert(array) + + pixel_data = array.tostring() # Recommended to specify all details # by http://www.pythonware.com/library/pil/handbook/image.htm - im = PIL.Image.frombuffer(mode, size, dataset.PixelData, + im = PIL.Image.frombuffer(mode, size, pixel_data, "raw", mode, 0, 1) - else: ew = dataset['WindowWidth'] ec = dataset['WindowCenter'] ww = int(ew.value[0] if ew.VM > 1 else ew.value) wc = int(ec.value[0] if ec.VM > 1 else ec.value) - image = get_LUT_value(dataset.pixel_array, ww, wc) - # Convert mode to L since LUT has only 256 values: - # http://www.pythonware.com/library/pil/handbook/image.htm + + ## Convert mode to L since LUT has only 256 values: + ## http://www.pythonware.com/library/pil/handbook/image.htm + image = get_LUT_value(array, ww, wc) im = PIL.Image.fromarray(image).convert('L') return im