This commit is contained in:
Ross
2021-02-14 17:58:25 +00:00
parent c8020bdba0
commit fc2c64cd76
3 changed files with 40 additions and 11 deletions
+25 -6
View File
@@ -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