Open alexbaras opened 3 months ago
Hi Alex, I followed your example above to get timestamp information from my czi file. At least I think this might be the only way to retrieve that data for every frame.
from io import BytesIO
import czifile
with czifile.CziFile(czi_file.as_posix()) as f:
# get attachment objects
attachments = {a.attachment_entry.name: {'obj': a} for a in f.attachments()}
attachments['TimeStamps']['obj']._fh.seek(attachments['TimeStamps']['obj'].data_offset)
with BytesIO(attachments['TimeStamps']['obj']._fh.read(attachments['TimeStamps']['obj'].data_size)) as fh:
attachments['TimeStamps']['img'] = czifile.CziFile(fh).asarray()
However I am running into this error
ValueError Traceback (most recent call last)
Cell In[1422], line 8
5 attachments['TimeStamps']['obj']._fh.seek(attachments['TimeStamps']['obj'].data_offset)
6 with BytesIO(attachments['TimeStamps']['obj']._fh.read(attachments['TimeStamps']['obj'].data_size)) as fh:
----> 8 attachments['TimeStamps']['img'] = czifile.CziFile(fh).asarray()
File [~/miniconda3/envs/suite2p/lib/python3.9/site-packages/czifile/czifile.py:266](http://localhost:8888/lab/tree/octo_code/calcium_imaging_slices/~/miniconda3/envs/suite2p/lib/python3.9/site-packages/czifile/czifile.py#line=265), in CziFile.__init__(self, arg, multifile, filesize, detectmosaic)
264 try:
265 if self._fh.read(10) != b'ZISRAWFILE':
--> 266 raise ValueError('not a CZI file')
267 self.header = Segment(self._fh, 0).data()
268 except Exception:
ValueError: not a CZI file
Do you have any ideas on how to solve that?
Hi all,
currently the functionality of redaing attachments & timestamps is not supported from pylibCZIrw. The typical workaround is to use other existing libraries like czifile or aicspylibczito read the attachments.
Example (use at your own risk) can be found here:
https://github.com/sebi06/czitools/blob/main/demo/scripts/read_attachment_images.py
(very similar appaoch to what @alexbaras posted above)
https://github.com/sebi06/czitools/blob/main/demo/notebooks/read_czi_metadata.ipynb
But I will add you feature request to our backlog.
Thank you @sebi06 . I found the solution for my problem here: https://github.com/cgohlke/czifile/issues/11 - this worked perfectly for me. Just posting it here in case others stumble over the same problem.
From my perspective the pylibczirw module is one of the only modules that allows for writing czi files. As such, I think it would be important to enable both reading and writing of czi files to include various associated images like slide label and slide / tissue preview.
For now I use czifile for access to the "attachments". I've posted that code example below. I suspect this example and the czifile module code base could be put together to achieve the desired effect.
-Alex
get czi attachments
with czifile.CziFile(czi_files[i_file]) as czi:
get attachment objects