1

I have a zip-file that contains .nrrd type files. The pynrrd lib comes with a read function. How can I pull the .nrrd file from the zip and pass it to the nrrd.read() function?

I tried following, but that gives the following error at the nrrd.read() line:

TypeError was unhandled by user code, file() argument 1 must be encoded string without NULL bytes, not str

in_dir = r'D:\Temp\Slikvideo\JPEG\SV_4_1_mask'
zip_file = 'Annotated.mitk'

zf = zipfile.ZipFile(in_dir + '\\' + zip_file)

f_name = 'datafile.nrrd'    # .nrrd file in zip

file_nrrd = zf.read(f_name)    # pull the file from the zip

img_nrrd, options = nrrd.read(file_nrrd)    # read the .nrrd image data from the file

I could write the file pulled from the .zip to disk, and then read it from disk with nrrd.read() but I am sure there is a better way.

JClarke
  • 788
  • 1
  • 9
  • 22
jdelange
  • 763
  • 2
  • 10
  • 22

2 Answers2

0

I think that your is a good way...

Here there is a similar question:

Similar question

Plus answer: I think that the problem maybe is that when you use zipfile.ZipFile you not set the attribute: Try using:

zipfile.ZipFile (path,"r")
Community
  • 1
  • 1
P.Carlino
  • 661
  • 5
  • 21
  • Adding "r" gives same error. I checked your Similar Question, but it's different as the file is passed to a pygame.load() function and the solution that is given is what I already have and does not work. – jdelange Aug 10 '16 at 15:18
0

The following works:

file_nrrd = zf.extract(f_name)    # extract the file from the zip
jdelange
  • 763
  • 2
  • 10
  • 22