Package: pydicom
pydicompydicomis a pure Python package for working withDICOMfiles.
It lets you read, modify and write DICOM data in an easy "pythonic" way. As a pure Python package,pydicomcan run anywhere Python runs without any other requirements, although if you're working
withPixel Datathen we recommend you also installNumPy.Note thatpydicomis 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 thepydicom organizationare based onpydicomand provide support for other aspects of DICOM, and for more
specific applications.Examples arepynetdicom, which
is a Python library for DICOM networking, anddeid,
which supports the anonymization of DICOM files.InstallationUsingpip:pip install pydicomUsingconda:conda install -c conda-forge pydicomFor more information, including installation instructions for the development version, see theinstallation guide.DocumentationThepydicomuser guide,tutorials,examplesandAPI referencedocumentation is available for both thecurrent releaseand thedevelopment versionon GitHub Pages.Pixel DataCompressed and uncompressedPixel Datais always available to
be read, changed and written asbytes:>>>frompydicomimportdcmread>>>frompydicom.dataimportget_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'IfNumPyis installed,Pixel Datacan be converted to anndarrayusing theDataset.pixel_arrayproperty:>>>arr=ds.pixel_array>>>arr.shape(128,128)>>>arrarray([[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)CompressedPixel DataJPEG, JPEG-LS and JPEG 2000Converting JPEG compressedPixel Datato anndarrayrequires installing one or more additional Python libraries. For information on which libraries are required, see thepixel data handler documentation.Compressing data into one of the JPEG formats is not currently supported.RLEEncoding and decoding RLEPixel Dataonly requires NumPy, however it can
be quite slow. You may want to considerinstalling one or more additional
Python librariesto speed up the process.ExamplesMoreexamplesare available in the documentation.Change a patient's IDfrompydicomimportdcmreadds=dcmread("/path/to/file.dcm")# Edit the (0010,0020) 'Patient ID' elementds.PatientID="12345678"ds.save_as("/path/to/file_updated.dcm")Display the Pixel DataWithNumPyandmatplotlibimportmatplotlib.pyplotaspltfrompydicomimportdcmreadfrompydicom.dataimportget_testdata_file# The path to a pydicom test datasetpath=get_testdata_file("CT_small.dcm")ds=dcmread(path)# `arr` is a numpy.ndarrayarr=ds.pixel_arrayplt.imshow(arr,cmap="gray")plt.show()ContributingWe are all volunteers working onpydicomin 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 ourcontribution guide.If you have examples or extensions ofpydicomthat don't belong with the
core software, but that you deem useful to others, you can add them to our
contribution repository:contrib-pydicom.