pydicom change PixelData of DICOM File

Created on 21 May 2020  路  3Comments  路  Source: pydicom/pydicom

Hey, I am currently trying to change the PixelData of my DICOM file, because I want to take over all tags from this DICOM file.
The DICOM file looks like this:

(0008, 0005) Specific Character Set              CS: 'ISO_IR 192'
(0008, 0008) Image Type                          CS: ['ORIGINAL', 'PRIMARY']
(0008, 0012) Instance Creation Date              DA: '20200317'
(0008, 0013) Instance Creation Time              TM: '110158.699'
(0008, 0016) SOP Class UID                       UI: VL Microscopic Image Storage
(0008, 0018) SOP Instance UID                    UI: 1.2.276.0.75.2.7.30.1.3.200317105525366.0.110625
(0008, 0020) Study Date                          DA: '20200317'
(0008, 0021) Series Date                         DA: '20200317'
(0008, 0022) Acquisition Date                    DA: '20200317'
(0008, 0023) Content Date                        DA: '20200317'
(0008, 002a) Acquisition DateTime                DT: '20200317105522'
(0008, 0030) Study Time                          TM: '105414'
(0008, 0031) Series Time                         TM: '105414'
(0008, 0032) Acquisition Time                    TM: '105522'
(0008, 0033) Content Time                        TM: '105522'
(0008, 0050) Accession Number                    SH: ''
(0008, 0060) Modality                            CS: 'GM'
(0008, 0070) Manufacturer                        LO: 'Carl Zeiss Meditec'
(0008, 0090) Referring Physician's Name          PN: ''
(0008, 1010) Station Name                        SH: 'CONVIVO'
(0008, 103e) Series Description                  LO: 'Confocal Image'
(0008, 1090) Manufacturer's Model Name           LO: 'CONVIVO'
(0010, 0010) Patient's Name                      PN: 'ANONYMOUS'
(0010, 0020) Patient ID                          LO: 'ANOMYMOUS'
(0010, 0030) Patient's Birth Date                DA: '19500101'
(0010, 0040) Patient's Sex                       CS: 'M'
(0018, 1000) Device Serial Number                LO: '6918000000'
(0018, 1020) Software Versions                   LO: ['', '', '']
(0018, 9004) Content Qualification               CS: 'RESEARCH'
(0020, 000d) Study Instance UID                  UI: 1.2.276.0.75.2.7.30.1.1.200317105409527.0.110622
(0020, 000e) Series Instance UID                 UI: 1.2.276.0.75.2.7.30.1.2.200317105412359.0.110623
(0020, 0010) Study ID                            SH: 'COVO-94a1271d21'
(0020, 0011) Series Number                       IS: "2"
(0020, 0013) Instance Number                     IS: "1"
(0020, 0020) Patient Orientation                 CS: ''
(0020, 4000) Image Comments                      LT: 'FocusDepth:0;Laser:0;AutoBrightness:On;Brightness:73;Gain:0;Filter:0;Scanresolution: 1920x1080;SpotCounter:0;SpotColor:#ffffff;'
(0028, 0002) Samples per Pixel                   US: 3
(0028, 0004) Photometric Interpretation          CS: 'YBR_FULL_422'
(0028, 0006) Planar Configuration                US: 0
(0028, 0010) Rows                                US: 1080
(0028, 0011) Columns                             US: 1920
(0028, 0100) Bits Allocated                      US: 8
(0028, 0101) Bits Stored                         US: 8
(0028, 0102) High Bit                            US: 7
(0028, 0103) Pixel Representation                US: 0
(0028, 0301) Burned In Annotation                CS: 'NO'
(0028, 2110) Lossy Image Compression             CS: '01'
(0040, 0555)  Acquisition Context Sequence   0 item(s) ---- 
(2201, 0010) Private Creator                     LO: '99CZM_NIM_INTERNAL_01'
(2201, 1000) Private tag data                    LT: 'VlMicroscopicImage'
(2201, 1001) Private tag data                    LT: '2.13'
(7fe0, 0010) Pixel Data                          OB: Array of 625376 elements

To change the pixel data I load a new image (1920, 1080, 3):

pixel_array = Image.open("path") 
test_file = dcmread("path") 
test_file.PixelData = pixel_array.tostring() 
test_file.saveas("test.dcm")`

Unfortunately I get an error:

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

I cannot interpret this error, the image that I am loading is not compressed. In comparison is:
Original: PixelData. - (OB) Array of 625376 elements
New image: PixelData - (OB) Array of 2073600 Elements

Thank you for your help!

pixel-data question

All 3 comments

Rows x columns x nr channels x bytes/px = 1920 x 1080 x 3 x 1 = 6220800 bytes. Since the length of Pixel Data in the original is 625376 then I'm pretty sure the original data is compressed in some fashion. Also, I doubt your new pixel data is correct since its not the right length. Unless your new image is in YCbCr colourspace then your Photometric Interpretation will also need to be changed.

Please post the transfer syntax UID: print(ds.file_meta.TransferSyntaxUID). Also, please ensure any data you post has been anonymised. I would try setting the transfer syntax appropriately before saving.

Thank you for the fast answer. Then I misunderstood the error message. But of course, based on the bytes in PixelData you can see that they are significantly less and the original image has been compressed.

The TransferSyntaxUID in the original file is:
TransferSyntaxUID = "1.2.840.10008.1.2.4.50"

The way I do the compression now doesn't make any changes yet, the error still comes and PixelData is the same size.

pixel_array = Image.open("path") 
test_file = dcmread("path") 
test_file.PixelData = pixel_array.tostring() 
test_file.file_meta.TransferSyntaxUID = "1.2.840.10008.1.2.4.50"
test_file.save_as("test.dcm")

Maybe the error is because the TransferSyntaxUID is for JPEG Compression and I麓m loading
.tif-images with pillow ?

If your pixel data is no longer compressed then you should change the transfer syntax to explicit VR little endian before saving:

from pydicom.uid import ExplicitVRLittleEndian

test_file.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian

If that doesn't work you can either try upgrading to the most recent release or manually set the flag that's causing the error (provided of course your pixel data really is uncompressed):

test_file['PixelData'].is_undefined_length = False
Was this page helpful?
0 / 5 - 0 ratings

Related issues

oliverzzm picture oliverzzm  路  12Comments

richard-moss picture richard-moss  路  8Comments

darcymason picture darcymason  路  9Comments

wanghaisheng picture wanghaisheng  路  8Comments

anxingle picture anxingle  路  4Comments