Cannot save composite image of two CT images

30 views
Skip to first unread message

Bahman Kasmai

unread,
Dec 1, 2022, 2:56:22 PM12/1/22
to pydicom
in the following code i read two CT images and then create a rgbArray which can be displayed by plt.imshow(rgbArray) or I could use image = Image.fromarray(rgbArray) to  with no problem as shown in the code below and attached screen capture:

ds = dicom.read_file('image1')
arr = ds.pixel_array
rows,columns= arr.shape
min= arr.min()
max= arr.max()
image1= (arr/(max-min))*255

ds = dicom.read_file('image2')
arr = ds.pixel_array
rows,columns= arr.shape
min= arr.min()
max= arr.max()
image2= (arr/(max-min))*255
rgbArray = np.zeros((rows,columns,3), 'uint8')
rgbArray[:,:, 0] = image1[:,:]
rgbArray[:,:, 1] = image2[:,:]
rgbArray[:,:, 2] =0
plt.imshow(rgbArray)
image = Image.fromarray(rgbArray)
image.show() 

I need to save this composute image as  secondary dicom image file by using the code below:

ds.PixelData = rgbArray.tobytes()
ds.PhotometricInterpretation = 'RGB'
ds.SamplesPerPixel = 3
ds.BitsAllocated = 8
ds.BitsStored = 8
ds.HighBit = 7
ds.save_as('composite.dcm)

The code fails on the last line: ds.save_as('composite.dcm)

Any help on this will be appreciated

composite.PNG

Darcy Mason

unread,
Dec 1, 2022, 4:47:20 PM12/1/22
to pydicom
Can you post the error message?  

Ahmad Dawalibi

unread,
Dec 1, 2022, 6:37:48 PM12/1/22
to pyd...@googlegroups.com
It could be silly, but did you check the syntax of the code? I can see you are missing a at the end after .dcm

ds.save_as('composite.dcm)


--
You received this message because you are subscribed to the Google Groups "pydicom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydicom+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pydicom/a9c972ea-5bd8-4047-9a29-d6d6b109b1ccn%40googlegroups.com.
--
Best Regards
Ahmad Dawalibi
HCIT Specialist
Mobile:+966-55-878-5213

Bahman Kasmai

unread,
Dec 2, 2022, 3:19:53 AM12/2/22
to pydicom
This was just a typo while posting. 

Bahman Kasmai

unread,
Dec 2, 2022, 3:24:55 AM12/2/22
to pydicom
Here is the error message:

Exception has occurred: ValueError

With tag (7fe0, 0010) got exception: (7FE0,0010) Pixel Data has an undefined length indicating that it's compressed, but the data isn't encapsulated as required. See pydicom.encaps.encapsulate() for more information Traceback (most recent call last): File "C:\Users\clinbk\AppData\Local\Programs\Python\Python310\lib\site-packages\pydicom\tag.py", line 28, in tag_in_exception yield File "C:\Users\clinbk\AppData\Local\Programs\Python\Python310\lib\site-packages\pydicom\filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "C:\Users\clinbk\AppData\Local\Programs\Python\Python310\lib\site-packages\pydicom\filewriter.py", line 579, in write_data_element raise ValueError( ValueError: (7FE0,0010) Pixel Data has an undefined length indicating that it's compressed, but the data isn't encapsulated as required. See pydicom.encaps.encapsulate() for more information

Bahman Kasmai

unread,
Dec 5, 2022, 6:31:03 AM12/5/22
to pydicom
I managed to find a solution from  https://github.com/pydicom/pydicom/issues/1186. The dicom file can be viewed by MicroDicom and ImageJ but other PACS browsers cannot make sense of it. Attached is the Dicom file. Any idea what is wrong with the attached file. 
problem.dcm

Darcy Mason

unread,
Dec 5, 2022, 12:16:04 PM12/5/22
to pydicom
Your bits and bytes don't seem to add up:  512*512*3*(8 BitsAllocated) does not equal the size of the PixelData. Somehow it looks like 16-bit was stored.

Bahman Kasmai

unread,
Dec 6, 2022, 4:57:41 AM12/6/22
to pydicom
When I try to read the dat dataset back by setting  force=True (read_file('myfile',force=True) ) I see that pixel array size is  786432 elements (512x512x3). I used a binary file viewer and note that the header does not contain the 'DICM' prefix. Surely save_as should take care of this? 

Bahman Kasmai

unread,
Dec 6, 2022, 5:10:32 AM12/6/22
to pydicom
Problem solved! All I needed to do was to set write_like_original=False in save_as('myfile.dcm',   write_like_original=False)
Reply all
Reply to author
Forward
0 new messages