Python Forum

Full Version: Trying to invert DICOM image with Python 3.6
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to INVERT a DICOM image in Python 3.6. I've tried this (and many others) pieces of code:

#from PIL import Image
import matplotlib.pyplot as plt
import PIL.ImageOps    
import os
import pydicom
os.chdir('C:\\Users\\franc\\Desktop\\ILD Swiss\\ILD_DB_lungMasks\\3\\lung_mask')
image=pydicom.dcmread("lung_mask_7358_10.dcm")
#image = image.open("lung_mask_7358_10.dcm")


plt.imshow(image.pixel_array, cmap=plt.cm.gray) 

inverted_image = PIL.ImageOps.invert(image)

#inverted_image.save('invert3.dicom')
I get this error:

Error:
File "<ipython-input-54-55e84423ace0>", line 1, in <module> runfile('C:/Users/franc/.spyder-py3/invert3.py', wdir='C:/Users/franc/.spyder-py3') File "C:\Users\franc\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "C:\Users\franc\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/franc/.spyder-py3/invert3.py", line 20, in <module> inverted_image = PIL.ImageOps.invert(image) File "C:\Users\franc\Anaconda3\lib\site-packages\PIL\ImageOps.py", line 392, in invert return _lut(image, lut) File "C:\Users\franc\Anaconda3\lib\site-packages\PIL\ImageOps.py", line 49, in _lut if image.mode == "P": File "C:\Users\franc\Anaconda3\lib\site-packages\pydicom\dataset.py", line 476, in __getattr__ return super(Dataset, self).__getattribute__(name) AttributeError: 'FileDataset' object has no attribute 'mode'
The image displays because of:

plt.imshow(image.pixel_array, cmap=plt.cm.gray)
I've tried seemingly everything. Some lines of code have been commented out to try to diagnose the problem.
I don't know pydicom, and very little of PIL, but obviously you passed an argument 'image' which type is 'pydicom.FileDataSet' and this is not the type that 'PIL.ImageOps.invert()' is expecting.