0

I'm trying to read dicom files in a zip folder but when I run this code it gives me this error:

[Errno 13] Permission denied: 'PATIENT_DICOM/'

file = patient.PATIENT_DICOM

with zipfile.ZipFile(file,'r') as zip:
    zip.extractall()
    ls= zip.infolist()
    slices = [pydicom.read_file(s.filename) for s in ls]
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141

2 Answers2

0

Basically, you have to read the extracted DICOM files:

zip_path = patient.PATIENT_DICOM
with zipfile.ZipFile(zip_path, 'r') as zip:
    path = tempfile.mkdtemp()
    zip.extractall(path)
    slices = []
    for root, _, filenames in os.walk(path):
        for filename in filenames:
            filepath = os.path.join(root, filename)
            slices.append(pydicom.dcmread(filepath))
    shutil.rmtree(path)

Note that this first extracts all files into a temp dir, which is probably faster than accessing them one by one. This assumes, that all files in the zip belong to the same volume or series. If you want to work with the slices further, you have to sort them properly first, for example by InstanceNumber.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
0

I think you have to try this:

data = pydicom.dcmread(filename)
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • `dcmread` is an alias for `read_file`, and while it is correct that it shall be used, it does not change the semantics. – MrBean Bremen Feb 12 '20 at 16:12