Python error with dicom.read_file()

pydicom
python dicom viewer
jpeg to dicom python
plot dicom files in python
decompress dicom python
convert dicom to jpeg python
python opencv dicom
png to dicom python

I am writing an app to do something with DICOM images with Python (using Pydicom library). Unfortunately I am stuck at the very beginning with a problem that shouldn't exist, according to the tutorials I read (f.e. here ).

The code is simple and goes like that:

import dicom
dicomImage = dicom.read_file(MRI_img.dcm)
(...) other commands

I installed Pydicom 0.9.9 with pip (it is visible when I use pip list. I am writing code in Ecplipse with PyDev plugin.

Text editor finds "read_file" part invalid, with "Undefined variable from import: read_file" comment, and compiler says:

AttributeError: 'module' object has no attribute 'read_file'

The weird thing is even when I directly copy some code from one of tutorials available on the Net, it stays the same. Using solution from that topic won't work either. I'm slowly running out of reasonable solutions, and I still have completly no idea what is wrong.

P.S. Opening example dicom reading code from dicom/examples folder:

import sys
import dicom

# check command line arguments make sense
if not 1 < len(sys.argv) < 4:
print(__doc__)
sys.exit()

# read the file
filename = sys.argv[1]
dataset = dicom.read_file(filename)
(...)

When executed from command line - it works with no problems. That leaves me with even less idea, what's wrong with my code.

Edit: well it seems that only Eclipse/PyDev has that problem, when I execute any code from command lines, or Qt IPython command line it all works without any prolem. So what could be the problem with Eclipse? Does any of you ever encounered it?

i just doing some research also and i got the exactly same error as you do, i found out that the reason is simple, and what happen to me that raise that error is, I named my file as dicom.py..... this dicom.py is the reason and you know what happen next, just rename it to testdicom.py or any name as long as not dicom.py and will solve your problem

dicom.read_file Python Example, This page provides Python code examples for dicom.read_file. The following are code examples for showing how to use dicom.read_file(). They are ds.​decode() # Make sure don't get unicode encode error on converting to string expected  The following are code examples for showing how to use dicom.read_file().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

For future information:

I haven't found a reason, but got a solution, if you have similar problem (Windows version):

First uninstall your current Python version, and delete remaining python folder Then download and install fresh Python(x,y) here (version 2.7.10 as for 26 jan 2016) Then enter Scripts folder in command prompt and enter pip uninstall pydcom Then download Pydicom from official site (1.0.0 as for today) Unzip, enter folder in command prompt and type python setup.py install Wait a few moments until library installs.

After that you can (at least I could) use pydicom library in PyDev without any problems, following code:

import pydicom
import numpy

dicomLoc = "C:\\MR000000.dcm"
ds = pydicom.read_file(dicomLoc)
print(ds.pixel_array)

Resulted in printing proper pixel array to console:

[[0 0 0 ..., 0 0 0]
 [0 0 0 ..., 0 0 1]
 [0 0 0 ..., 0 0 0]
 ..., 
 [0 3 0 ..., 0 2 5]
 [0 9 3 ..., 1 4 4]
 [0 2 0 ..., 0 7 1]]

No further Pydicom or PyDev problems spotted since that moment.

Error with exception when reading a file · Issue #331 · pydicom , exists): In [1]: import pydicom In [2]: d = pydicom.read_file('image-000001.dcm') In _get_pixel_array() 710 except AttributeError: 711 t, e, I have the same error with pillow 5.1.0, python 3.6, pydicom 1.2.1, and the ocr cleaner has bug with gcc library / scikit image version pydicom/dicom-cleaner#9. Using "force=True" does fix the problem: dicom.read_file() succeeds and I can read tags such as SeriesDescription. These DICOM files indeed miss a DICOM header and specifically the prefix "DICM" at position 0x80:

I think you missed something quotes, so the error exist.

The code should like this:

dicomImage = dicom.read_file("MRI_img.dcm");

My Dicom files are missing the (0002, xxxx) lines. · Issue #316 , I have DICOM files of a CT scanner which misses the 0002 tags. 0.9.9 then the only difference is you import dicom and call dicom.read_file(. At a minimum you need the following elements in order to use Dataset.save_as() : then unzip it and run python setup.py install in the pydicom directory to install  2 Answers2. Active Oldest Votes. 1. you're passing the first char of the string ( C) instead of the full string. Just do: ds=dicom.read_file(filePath) next error you'll stumble into: use raw prefix or \t gets interpreted as a tabulation character: filePath=r"C:\Python34\Lib\site-packages\dicom\testfiles" ^. share.

You want to use pydicom and your code is

import dicom
dicomImage = dicom.read_file(MRI_img.dcm)

Actually, the correct code should like this

import pydicom
dicomImage = pydicom.read_file('MRI_img.dcm')

Pydicom User Guide, import dicom >>> ds = dicom.read_file("rtplan.dcm") # (rtplan.dcm is in the a handy dir() method, useful during interactive sessions at the python prompt: >  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to fix encoding issues of pydicom in python

Loading DICOM files in Python, For Python 3.4, pydicom 0.99, when I try loading DICOM files, I get this error: ds​=dicom.read_file(filePath[0]) Error is: C:\Python34\python.exe  pydicom. pydicom is a pure python package for working with DICOM files. It was made for inspecting and modifying DICOM data in an easy "pythonic" way. The modifications can be written again to a new file. As a pure python package, pydicom can run anywhere python runs without any other requirements, although NumPy is needed if manipulating pixel

Problem writing file after reading it with pydicom, 2009-12-03 15:00:24 ScriptError: filewriter.py(248) error, see below 2009-12-03 15:00:24 Is there a bug in "dicom.read_file()" or am I missing something? Andrei. Sign in to reply module was a non-python one. Many thanks for your quick  A Python framework for decoding JPEG images, with a focus on supporting pydicom python jpg jpeg decoding dicom jp2 jpeg2000 Python MIT 1 6 4 0 Updated Jun 15, 2020

dicom.read_file Example, Here are the examples of the python api dicom.read_file taken from open source projects. By voting up ds = dicom.read_file(os.path.join(testfiles_dir(), "rtplan.​dcm" )). print "sending print ( "!!!Error, check, slocation calculated is different" );. Dataset (derived from python’s dict) —> contains DataElement instances –> the value of the data element can be one of: a regular value like a number, string, etc. a list of regular values (e.g. a 3-D coordinate) a Sequence instance –> a Sequence is a list of Datasets (and so we come full circle)

Comments
  • can you add eclipse tag? also have you installed since restarting eclipse? I know I have that issue with IDLE
  • Yes, I restarted eclipse, reinstalled pydicom library, everything except pydicom modules works fine (numpy, simpleITK, etc.)
  • Sorry I'm having issues just installing PyDev for eclipse, wish you luck resolving your problem though!
  • Well, thank you, I will notify if I will find any solution, now it is 100% sure that problem is with pydicom library, something is wrong with it on Eclipse.
  • alright, just found out there is also a PyDev tag which you should add to attract more knowledgeable people, I couldn't even install PyDev on my computer :( Good luck finding a solution!
  • absolutely true, if you name your file similar to the library it creates a conflict, thanks for pointing this out