How to create dicom image from scratch

256 views
Skip to first unread message

dt...@hotmail.com

unread,
Dec 3, 2009, 4:45:06 AM12/3/09
to pydicom
Hi,

I am constructing 3d images in python and would like to write out to
dicom format using pydicom. How would I create these dicom files from
scratch? 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\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\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\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
------------------------------------------------------------------------------------------------
0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000080: 4449 434d 0200 0000 554c 0400 8400 0000 DICM....UL......
0000090: 0200 0100 4f42 0000 0200 0000 0001 0200 ....OB..........
00000a0: 0200 5549 1a00 312e 322e 3834 302e 3130 ..UI..1.2.840.10
00000b0: 3030 382e 352e 312e 342e 312e 312e 3400 008.5.1.4.1.1.4.
00000c0: 0200 0300 5549 2000 3939 392e 3939 392e ....UI .999.999.
00000d0: 322e 3139 3936 3036 3139 2e31 3633 3030 2.19960619.16300
00000e0: 302e 312e 3130 3300 0200 1000 5549 1400 0.1.103.....UI..
00000f0: 312e 322e 3834 302e 3130 3030 382e 312e 1.2.840.10008.1.
0000100: 322e 3100 0200 1200 5549 0800 3939 392e 2.1.....UI..999.
0000110: 3939 3900 1800 5000 4453 0400 3235 3020 999...P.DS..250
0000120: 2800 0200 5553 0200 0100 2800 0400 4353 (...US....(...CS
0000130: 0c00 4d4f 4e4f 4348 524f 4d45 3220 2800 ..MONOCHROME2 (.
0000140: 0800 4953 0200 3220 2800 1000 5553 0200 ..IS..2 (...US..
0000150: 1000 2800 1100 5553 0200 0800 2800 0001 ..(...US....(...
0000160: 5553 0200 0800 2800 0101 5553 0200 0800 US....(...US....
0000170: 2800 0201 5553 0200 0700 2800 0301 5553 (...US....(...US
0000180: 0200 0000 e07f 1000 4f57 2f4f 4200 0100 ........OW/OB...
0000190: 1020 3040 5060 7001 1121 3141 5161 7102 . 0@P`p..!1AQaq.
00001a0: 1222 3242 5262 7203 1323 3343 5363 7304 ."2BRbr..#3CScs.
00001b0: 1424 3444 5464 7405 1525 3545 5565 7506 .$4DTdt..%5EUeu.
00001c0: 1626 3646 5666 7607 1727 3747 5767 7708 .&6FVfv..'7GWgw.
00001d0: 1828 3848 5868 7809 1929 3949 5969 790a .(8HXhx..)9IYiy.
00001e0: 1a2a 3a4a 5a6a 7a0b 1b2b 3b4b 5b6b 7b0c .*:JZjz..+;K[k{.
00001f0: 1c2c 3c4c 5c6c 7c0d 1d2d 3d4d 5d6d 7d0e .,<L\l|..-=M]m}.
0000200: 1e2e 3e4e 5e6e 7e0f 1f2f 3f4f 5f6f 7f80 ..>N^n~../?O_o..
0000210: 90a0 b0c0 d0e0 f081 91a1 b1c1 d1e1 f182 ................
0000220: 92a2 b2c2 d2e2 f283 93a3 b3c3 d3e3 f384 ................
0000230: 94a4 b4c4 d4e4 f485 95a5 b5c5 d5e5 f586 ................
0000240: 96a6 b6c6 d6e6 f687 97a7 b7c7 d7e7 f788 ................
0000250: 98a8 b8c8 d8e8 f889 99a9 b9c9 d9e9 f98a ................
0000260: 9aaa baca daea fa8b 9bab bbcb dbeb fb8c ................
0000270: 9cac bccc dcec fc8d 9dad bdcd dded fd8e ................
0000280: 9eae bece deee fe8f 9faf bfcf dfef ff0a ................

Darcy Mason

unread,
Dec 7, 2009, 8:58:35 PM12/7/09
to pydicom

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
Reply all
Reply to author
Forward
0 new messages