.
This commit is contained in:
+14
-4
@@ -6,7 +6,11 @@ from io import BytesIO
|
||||
from PIL import Image, ImageFile
|
||||
|
||||
import pydicom
|
||||
|
||||
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()
|
||||
+25
-6
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user