.
This commit is contained in:
+15
-5
@@ -6,7 +6,11 @@ from io import BytesIO
|
|||||||
from PIL import Image, ImageFile
|
from PIL import Image, ImageFile
|
||||||
|
|
||||||
import pydicom
|
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):
|
def image_as_base64(image_file):
|
||||||
"""
|
"""
|
||||||
@@ -42,11 +46,11 @@ def pil_dicom_image(source, exif_orientation=True, **options):
|
|||||||
# return
|
# return
|
||||||
#source = BytesIO(source.read())
|
#source = BytesIO(source.read())
|
||||||
|
|
||||||
print("start pdi")
|
# open file with pydicom
|
||||||
|
|
||||||
ds = pydicom.read_file(source)
|
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:
|
#with Image.open(source) as image:
|
||||||
# # Fully load the image now to catch any problems with the image contents.
|
# # 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:
|
#if exif_orientation:
|
||||||
# image = utils.exif_orientation(image)
|
# 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()
|
||||||
+25
-6
@@ -23,7 +23,10 @@ and Python Imaging Library:
|
|||||||
|
|
||||||
have_PIL = True
|
have_PIL = True
|
||||||
try:
|
try:
|
||||||
import PIL.Image
|
import PIL.Image, PIL.ImageOps
|
||||||
|
|
||||||
|
from pydicom.pixel_data_handlers.util import apply_voi_lut, apply_modality_lut
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
have_PIL = False
|
have_PIL = False
|
||||||
|
|
||||||
@@ -59,6 +62,13 @@ def get_PIL_image(dataset):
|
|||||||
if ('PixelData' not in dataset):
|
if ('PixelData' not in dataset):
|
||||||
raise TypeError("Cannot show image -- DICOM dataset does not have "
|
raise TypeError("Cannot show image -- DICOM dataset does not have "
|
||||||
"pixel data")
|
"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
|
# can only apply LUT if these window info exists
|
||||||
if ('WindowWidth' not in dataset) or ('WindowCenter' not in dataset):
|
if ('WindowWidth' not in dataset) or ('WindowCenter' not in dataset):
|
||||||
bits = dataset.BitsAllocated
|
bits = dataset.BitsAllocated
|
||||||
@@ -79,19 +89,28 @@ def get_PIL_image(dataset):
|
|||||||
# PIL size = (width, height)
|
# PIL size = (width, height)
|
||||||
size = (dataset.Columns, dataset.Rows)
|
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
|
# Recommended to specify all details
|
||||||
# by http://www.pythonware.com/library/pil/handbook/image.htm
|
# 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)
|
"raw", mode, 0, 1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
ew = dataset['WindowWidth']
|
ew = dataset['WindowWidth']
|
||||||
ec = dataset['WindowCenter']
|
ec = dataset['WindowCenter']
|
||||||
ww = int(ew.value[0] if ew.VM > 1 else ew.value)
|
ww = int(ew.value[0] if ew.VM > 1 else ew.value)
|
||||||
wc = int(ec.value[0] if ec.VM > 1 else ec.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:
|
## Convert mode to L since LUT has only 256 values:
|
||||||
# http://www.pythonware.com/library/pil/handbook/image.htm
|
## http://www.pythonware.com/library/pil/handbook/image.htm
|
||||||
|
image = get_LUT_value(array, ww, wc)
|
||||||
im = PIL.Image.fromarray(image).convert('L')
|
im = PIL.Image.fromarray(image).convert('L')
|
||||||
|
|
||||||
return im
|
return im
|
||||||
|
|||||||
Reference in New Issue
Block a user