On Dec 3, 4:45 am, "
dt...@hotmail.com" <
dt...@hotmail.com> wrote:
> ... I'm having problems using the following code. If I try to
> read in this file, I get the error
>
> Unexpected end of file. Read 255 bytes of 65602 expected starting at
> position 0x190.
>
> --------------------------------------------------------------------------- ---------------------
> user code:
> --------------------------------------------------------------------------- ---------------------
> ds = dicom.dataset.Dataset();
> ds.preamble='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ';
> ds.AddNew((0x2,0x2),'UI','1.2.840.10008.5.1.4.1.1.4');
> ds.AddNew((0x2,0x12),'UI','999.999');
> ds.AddNew((0x2,0x3),'UI','999.999.2.19960619.163000.1.103');
> ds.TransferSyntaxUID = '1.2.840.10008.1.2.1'
> ds.SliceThickness = dz;
> ds.SamplesperPixel = 1;
> ds.PhotometricInterpretation = 'MONOCHROME2';
> ds.NumberofFrames = shape(vol)[0];
> ds.Rows = shape(vol)[2];
> ds.Columns = shape(vol)[1];
> ds.BitsAllocated = 8;
> ds.BitsStored = 8;
> ds.HighBit = 7;
> ds.PixelRepresentation = 0;
> ds.isExplicitVR = True;
> ds.isLittleEndian = True;
> ds.AddNew((0x7fe0,0x0),'UL',len(ds.PixelData)+12)
> ds.PixelData = asarray(transpose(vol,(0,2,1)),uint8).tostring()
>
> dicom.write_file(filename,ds);
>
> --------------------------------------------------------------------------- ---------------------
> generated dicom file
> --------------------------------------------------------------------------- ---------------------
> ...
> 0000180: 0200 0000 e07f 1000 4f57 2f4f 4200 0100 ........OW/OB...
> ...
The problem in the file is in the one binary line I kept quoted above.
The DICOM dictionary has some ambigous VR entries, like "OW/OB" for
pixel data. When the dataset was written out as Explicit VR, pydicom
wrote the whole (more than 2 character) VR to the file, which of
course causes an error for any DICOM reader trying to read it back in.
I've entered this on the issue list (issue 65) [1].
In this case, adding the line:
ds.data_element("PixelData").VR = 'OB'
before writing the file made this go away for me, by forcing that
item's VR to be a proper two-character VR.
Hope this helps,
Darcy
[1]
http://code.google.com/p/pydicom/issues/detail?id=65