Code Monkey home page Code Monkey logo

pydicom's Introduction

unit-tests type-hints doc-build test-coverage Python version PyPI version DOI

pydicom

pydicom is a pure Python package for working with DICOM files. It lets you read, modify and write DICOM data in an easy "pythonic" way. As a pure Python package, pydicom can run anywhere Python runs without any other requirements, although if you're working with Pixel Data then we recommend you also install NumPy.

Note that pydicom is a general-purpose DICOM framework concerned with reading and writing DICOM datasets. In order to keep the project manageable, it does not handle the specifics of individual SOP classes or other aspects of DICOM. Other libraries both inside and outside the pydicom organization are based on pydicom and provide support for other aspects of DICOM, and for more specific applications.

Examples are pynetdicom, which is a Python library for DICOM networking, and deid, which supports the anonymization of DICOM files.

Installation

Using pip:

pip install pydicom

Using conda:

conda install -c conda-forge pydicom

For more information, including installation instructions for the development version, see the installation guide.

Documentation

The pydicom user guide, tutorials, examples and API reference documentation is available for both the current release and the development version on GitHub Pages.

Pixel Data

Compressed and uncompressed Pixel Data is always available to be read, changed and written as bytes:

>>> from pydicom import dcmread
>>> from pydicom.data import get_testdata_file
>>> path = get_testdata_file("CT_small.dcm")
>>> ds = dcmread(path)
>>> type(ds.PixelData)
<class 'bytes'>
>>> len(ds.PixelData)
32768
>>> ds.PixelData[:2]
b'\xaf\x00'

If NumPy is installed, Pixel Data can be converted to an ndarray using the Dataset.pixel_array property:

>>> arr = ds.pixel_array
>>> arr.shape
(128, 128)
>>> arr
array([[175, 180, 166, ..., 203, 207, 216],
       [186, 183, 157, ..., 181, 190, 239],
       [184, 180, 171, ..., 152, 164, 235],
       ...,
       [906, 910, 923, ..., 922, 929, 927],
       [914, 954, 938, ..., 942, 925, 905],
       [959, 955, 916, ..., 911, 904, 909]], dtype=int16)

Decompressing Pixel Data

JPEG, JPEG-LS and JPEG 2000

Converting JPEG, JPEG-LS or JPEG 2000 compressed Pixel Data to an ndarray requires installing one or more additional Python libraries. For information on which libraries are required, see the pixel data handler documentation.

RLE

Decompressing RLE Pixel Data only requires NumPy, however it can be quite slow. You may want to consider installing one or more additional Python libraries to speed up the process.

Compressing Pixel Data

Information on compressing Pixel Data using one of the below formats can be found in the corresponding encoding guides. These guides cover the specific requirements for each encoding method and we recommend you be familiar with them when performing image compression.

JPEG-LS, JPEG 2000

Compressing image data from an ndarray or bytes object to JPEG-LS or JPEG 2000 requires installing the following:

RLE

Compressing using RLE requires no additional packages but can be quite slow. It can be sped up by installing pylibjpeg with the pylibjpeg-rle plugin, or gdcm.

Examples

More examples are available in the documentation.

Change a patient's ID

from pydicom import dcmread

ds = dcmread("/path/to/file.dcm")
# Edit the (0010,0020) 'Patient ID' element
ds.PatientID = "12345678"
ds.save_as("/path/to/file_updated.dcm")

Display the Pixel Data

With NumPy and matplotlib

import matplotlib.pyplot as plt
from pydicom import dcmread
from pydicom.data import get_testdata_file

# The path to a pydicom test dataset
path = get_testdata_file("CT_small.dcm")
ds = dcmread(path)
# `arr` is a numpy.ndarray
arr = ds.pixel_array

plt.imshow(arr, cmap="gray")
plt.show()

Contributing

We are all volunteers working on pydicom in our free time. As our resources are limited, we very much value your contributions, be it bug fixes, new core features, or documentation improvements. For more information, please read our contribution guide.

If you have examples or extensions of pydicom that don't belong with the core software, but that you deem useful to others, you can add them to our contribution repository: contrib-pydicom.

pydicom's People

Contributors

astrofrog avatar bastula avatar blairconrad avatar cancan101 avatar cpbridge avatar darcymason avatar dependabot[bot] avatar dimitripapadopoulos avatar fmorency avatar glemaitre avatar hackermd avatar jrkerns avatar korijn avatar kwsp avatar massich avatar mmattes avatar moloney avatar mrbean-bremen avatar mshunshin avatar pawelzajdel avatar pre-commit-ci[bot] avatar rhaxton avatar scaramallion avatar simonbiggs avatar suever avatar thomsentner avatar timcogan avatar vsoch avatar youngkiu avatar zvibaratz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pydicom's Issues

PixelArray property not found when error raised in _getPixelArray

From [email protected] on May 04, 2009 15:42:48

What steps will reproduce the problem? 1. read_file() for dicom file without header info
2. didn't add ds.TransferSyntaxUID checked in PixelArray (corrected in r109 )
3. PixelArray reported as not a member of Dataset

So, root problem is that raised errors were somehow silenced ... and wound
up going to getattr to find a property which should have existed.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=42

Extend pydicom to earlier releases of python

From [email protected] on September 07, 2008 23:09:24

  1. The package fails to import on earlier versions of python (seen on 2.3.5
    on Windows)
    {{{
    File "C:\Python23\Lib\site-packages\dicom\tag.py", line 28, in new
    raise OverflowError, "Tags are limited to 32-bit length"
    OverflowError: Tags are limited to 32-bit length
    }}}
  2. setup.py script also uses package_data which python docs claim is new in
    python 2.4.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=1

Quick reading of file info, avoiding large items

From [email protected] on December 01, 2008 17:35:29

filereader.ReadFileMetaInfo() can avoid reading a whole file but only gives
limited information. Should add the ability to still read all information
but delay reading of large values (e.g. pixels, dose grid) until actually
needed.
Possible solution: have a flag in Attribute instance to mark whether has
actually been read in. A danger of this is that one has to re-open the file
to read those values ... if any changes have been made to the file then
this could be dangerous. Will need a flag for date/time stamp and/or
recheck the instance UID to make sure is same file.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=28

Failure to write VR='US or SS or OW' properly

From [email protected] on April 24, 2009 10:30:43

I ran into this problem while working with some new imagery.

With the attached imagery:
dcm = dicom.ReadFile( 'img3.dcm' )
dicom.WriteFile( 'img3out.dcm', dcm )

You receive "NotImplementedError: WriteDataElement: unknown Value
Representation 'US or SS or OW'".

This error raises when trying to write the tag (0028, 3006) LUTData.
Clearly the writing for this VR is not implemented, but easy to fix since
US, SS and OW are all some binary 16 bit values (ss and us have VM of 2,
where OW can be a large buffer of values).

Note: The following solution works for my needs under limited testing.

First, since US and SS both use write_OWvalue in filewriter.py, add 'US or
SS or OW':write_OWvalue to the writers dictionary.

Now pydicom will most likely raise an error in the write_OWvalue method
because it sees a list instead of a buffer of shorts. This is because the
DataElement constructor does not correctly capture the data for VR 'US or
SS or OW'.. It treats the buffer as a free text type VR and splits on the
text delimiter giving you a list of strings if the delimiter exists within
your data.. To treat the data as binary, do the following:

in the DataElement Constructor:
under _setvalue(self,val)
if isString(val) and self.VR not in
['UT','ST','LT', 'FL','FD','AT','OB','OW','OF','SL','SQ','SS',
'UL','US', 'OW/OB', 'UN', ## ADD THIS ->## 'US or SS or OW']:

Now this should work:
dcm = dicom.ReadFile( 'img3.dcm' )
dicom.WriteFile( 'img3out.dcm', dcm )

Attachment: img3.zip

Original issue: http://code.google.com/p/pydicom/issues/detail?id=41

Expand allowable types for tag creation

From [email protected] on May 27, 2009 22:09:18

Allow Tags to be created from lists (rather than just tuples) or perhaps
any object which can "unpack" into two items:
group, elem = some_object

Implementation:
In tag.py, replace isinstance check for tuple to a try-except block using
unpack line like above. Write unit tests in test_tag.py to at least add a
list instance to the tests of constructing tag instances. Add tests to
test_dataset.py testContains, at least one for a list like "[0x10,0x10] in
ds".

Original issue: http://code.google.com/p/pydicom/issues/detail?id=47

Update dictionary to 2008 standard

From [email protected] on September 15, 2008 23:34:13

Dictionary based on 03 standard. Need to update to 2008 documents. This
will entail:

  • adding tags to the dictionary
  • dealing with more "xx" groups and "xxx" elements
  • will update dictionary to include flag for whether the tag is retired
  • add a boolean attibute property isRetired
  • ? consider one-time pickling operation to speed loading since
    dictionary will be much larger

Original issue: http://code.google.com/p/pydicom/issues/detail?id=10

Add dictionaries for private tags where possible

From [email protected] on January 24, 2009 11:04:43

I think I saw some published dictionaries for private tags from some of the
vendors -- e.g. Siemens, GE, Philips. "It would be nice" to have
dictionaries available for such private tags.

Needs a little thought since tag numbers could conflict between different
vendors. May have to check the tags near start of dataset (e.g.
Manufacturer (0008,0070)?) and attach only that vendor's private tags
dictionary to the dataset instance somehow.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=36

Automate testing (including example programs) for package releases

From [email protected] on May 19, 2009 21:38:50

To simplify package release, need automated tests (ideally unit tests, but
possibly scripts) to run the example programs (in examples directory) and:

  • at least ensure they do not throw errors
  • ideally, verify that they give the expected results

As unit tests,

  • create test_examples.py file in "test" directory from copy of an existing
    testset
  • use execfile to run programs?
  • capture stderr and ensure no errors thrown?
  • capture stdout and sample for some expected printed values?

Have to consider how to run GUI examples -- i.e. dicomtree.py. May not be
able to automate checking, in which case it should not go in unittests.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=46

Incorrectly populates with Null Procedure Code Sequence

From [email protected] on November 24, 2008 14:40:43

  1. I'm working with mammography data from GE Senographe Essential burned
    from the Review Workstation. There is a null sequence (0008, 1032)
    ProcedureCodeSequence. pydicom seems to place all tags past that sequence
    inside of it such that PatientsName, PatientsID, etc.. are considered part
    of the sequence instead of in the document root as they should be.

I loaded the file in other dicom apps (Jdicom, ClearCanvas, Matlab), and
all apps parse these files correctly. Perhaps there is a bug handling null
sequences?

Using pydicom 0.9.1, WinXP 32-bit Python 2.5.2

Original issue: http://code.google.com/p/pydicom/issues/detail?id=27

Private tag VR UN loading as list instead of string

From [email protected] on December 18, 2008 17:21:11

Using the following code on the provided image:

dcm = dicom.ReadFile( 'image.dcm' ) # Attached to this issue as 7-zip
dcm.PatientsName = 'Blah'
dcm.SaveAs( 'image2.dcm' )

Gives the following exception:
argument 1 must be string or buffer, not list

The save fails because tag (0045,1029) is a VR=UN with VM=1, yet the values
are stored in a list (must be stored in string to save). The value stored
in the dicom file is a 'delimited' value of "1356\1" which is why I'm sure
it's being stored as the list ["1356", "1"]. However, if the VM is 1,
and/or VR type UN requires a string to save, then the loading code should
load the value "1356\1" as a string literal and not a python list.

Attachment: image.7z

Original issue: http://code.google.com/p/pydicom/issues/detail?id=33

Harmonize pixel data representations

From [email protected] on May 27, 2009 22:58:02

A pydicom "gotcha" comes from disconnect between Dataset.pixel_array
property and Dataset.PixelData. The latter is actually in the Dataset (a
dict), the former is created from it but changes are not written back
unless ds.PixelData is explicitly set with e.g. pixel_array.tostring().

Possible solutions:

  • always require Numpy, always convert pixel data into numpy array. Problem
    is this requires decompressing JPEG data (which is not available yet in
    pydicom and would possibly waste time on a step that might never be used if
    the code is not modifying pixels).
  • link pixel_array and PixelData together. if pixel_array is asked for,
    keep reference to it in dataset instance and automatically do tostring()
    before data is written. But what if user modified pixel_array and modified
    PixelData directly (current code would do that using the tostring() method
    mentioned above). Could see which one was modified most recently and use
    that as the definitive final value, or could throw an error or warning of
    possible inconsistent pixel data changes.
    This idea means PixelData probably needs to be redefined as a property so
    can flag writes to it. That makes it 'special' and different than other
    items in the Dataset dict, but perhaps that is necessary.

I like the second idea better, but am hoping someone can come up with an
even cleaner solution.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=49

Change package name to pydicom

From [email protected] on May 17, 2009 14:12:54

It's considered bad practice for the library name and it's import name to
be different -- it is confusing for users.

So, should change the import name to "pydicom", which would require:

  • update all documentation to use "pydicom" rather than "dicom"
  • update all example files
  • ?possibly provide a converter tool to search and replace:
    • from dicom import --> from pydicom import
    • import dicom --> import pydicom
    • dicom. --> pydicom.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=45

Pyglet connection or documentation

From [email protected] on May 27, 2009 22:18:44

As suggested by brandon.forrester in issue35 , could look into connection of
pydicom to pyglet ( http://www.pyglet.org/ ). It's a good fit with pydicom --
simple installation, very open (BSD) license.

This issue may not require much addition/modification of pydicom code --
perhaps documentation may be sufficient (could be added to WorkingWithPixelData page).

Original issue: http://code.google.com/p/pydicom/issues/detail?id=48

Delete data element by name

From [email protected] on June 11, 2009 22:26:44

As outline in PydicomUserGuide , can delete a data element from a dataset using
the tag. But should be able to do so using the name, just as for getting and
setting DICOM data elements, e.g. del ds.PatientId

Steps:

  • add delattr method to Dataset in dataset.py:
    (The code should be similar to that in setattr)
    • check if attribute already exists (i.e. is not a DICOM name), delete
      normally if it is.
    • If not a normal attribute, then get tag by TagForName(name) and delete the
      tag from the dataset if it exists; if not, raise appropriate error
      (AttributeError)
  • write unit tests -- need to test delete of attributes that exist and that
    attempt to del one that doesn't exist raises AttributeError
  • modify example programs that delete data elements (anonymize.py for sure;
    others?) to use the new format with the name

Then I will need to also edit the deletion part of the PydicomUserGuide to
show how to use the new method.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=50

Write missing meta info if not found in read-in file

From [email protected] on September 30, 2008 23:26:08

What steps will reproduce the problem? 1. Read a dicom file which is non-standard in not having file meta info
(preamble and group 2)
2. Write it out again.

Current code will also write without the meta info. Need to put in required
tags using pydicom's own Implementation Class UID, (optional) version, etc.
Group 2 elements [0, 1, 2, 3, 0x10, 0x12] are required by the DICOM standard.

Should also have a switch to put pydicom Implementation Class UID in even
if read-in file had proper meta info, as pydicom will have been the last to
process the file.

Original issue: http://code.google.com/p/pydicom/issues/detail?id=14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.