0

I'm reading CZI images with the first code block, successfully. I want to read xx.czi.gz with the second code block, but failed. How can I read 'czi.gz' in to an array?

Thanks a lot!

from czifile import CziFile
import gzip

fname='xxx.czi'
with CziFile(fname) as czi:
  image_arrays = czi.asarray()

fname2='xxx.czi.gz'
with gzip.open(fname2, 'rb') as f:
  with CziFile(f) as czi:
    image_arrays = czi.asarray()
Rui Li
  • 41
  • 1
  • 8
  • 1
    Looks fine. What's the issue? – Nick ODell Jan 10 '19 at 16:58
  • Thanks for you reply, the issue was, using the second chunk of code, the elements in image_arrays are all zeros. On the other hand, if I manually gunzip the file, and read with code chunk1, their are images there. I have no idea what is going on. – Rui Li Jan 10 '19 at 21:59

2 Answers2

1

I think the problem is that CziFile expect a file path. In case 2, you give it a file descriptor. I didn't see a method to initialize from a stream/fd. You might have to unzip then save the gz before reading the czi

  • Thanks Nico, I'll do that then. Maybe I want to do this manually before I run the code. Because I don't want to run two instances and have file errors? – Rui Li Jan 11 '19 at 13:23
0

I think @Nico238 is correct - you need to unzip the file first. However, you don't need to do that manually. You can do it with your Python program. That would look like this:

import shutils
import gzip
from czifile import CziFile
fname='xxx.czi'
fname2='xxx.czi.gz'

# decompress fname2 and put it in fname
with gzip.open(fname2, 'rb') as f_in:
  with open(fname, 'wb') as f_out:
    shutil.copyfileobj(f_in, f_out)

with CziFile(fname) as czi:
  image_arrays = czi.asarray()

(Source.)

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Thanks Nick! your code should work well. However, say if I have two codes running at the same time. Will I run into some problems as both programs are trying to unzip the same file and write into the same file name? – Rui Li Jan 14 '19 at 19:28
  • It might be a problem, depending on how you do it. Can you post some example code to show me what you mean? – Nick ODell Jan 15 '19 at 00:04