import os
import fabio
import numpy as np
def generate_tiff_files(fn, path, prefix, compress):
"""
Generate tiff files from a hdf file.
:param fn, metadata, path, prefix:
:return: -
"""
print('Generating TIFF Files...')
with fabio.open(fn) as fabio_img:
# create_tiff(fabio_img.data, metadata, path, prefix, 1)
create_tiff(fabio_img, path, prefix, 1, compress)
if fabio_img.nframes > 1:
for i in range(2, fabio_img.nframes + 1):
fabio_img = fabio_img.next()
# create_tiff(fabio_img.data, metadata, path, prefix, i)
create_tiff(fabio_img, path, prefix, i, compress)
log_progress(i, fabio_img.nframes)
print('Completed')
generate_tiff_files(r'C:\Users\wayne\OneDrive\Desktop\Test_hdf5_to_tiff\rat8_trabec1_225_data_000001.h5', r'rat8_trabec1_225_data_000001.h5', r'C:\Users\wayne\OneDrive\Desktop\Test_hdf5_to_tiff', False)
這段程式碼是我在這個網站上找到的, 他寫說是可以把 h5轉成tiff檔. Convert HDF5 file to TIFF files — MuscleX 1.15.7 documentation
Source code在這: https://github.com/biocatiit/musclex/blob/master/musclex/utils/hdf5_to_tiffs.py
我嘗試要把h5的檔案轉成tiff檔,, 裝了Fabio 的 package 後試著跑了第二段的程式碼但卻跑出下面的錯誤訊息, 嘗試在stack overflow 上找過了可是大部分都說是因為檔案受損導致,我用確定沒受損的檔案也照樣會跑出錯誤訊息. 小弟我嘗試用關鍵字找但還是不知道為什麼fabio沒辦法讀h5檔, 我是這學期才剛開始學python, 還不是很熟悉. 請問我到底要怎麼辦才能讓fabio正常讀我想要的h5檔?
錯誤訊息:
Generating TIFF Files...
Traceback (most recent call last):
File "C:\Users\wayne\OneDrive\Desktop\Test_hdf5_to_tiff\hdf5_to_tiffs.py", line 120, in <module>
generate_tiff_files(r'C:\Users\wayne\OneDrive\Desktop\Test_hdf5_to_tiff\rat8_trabec1_225_data_000001.h5', r'rat8_trabec1_225_data_000001.h5', r'C:\Users\wayne\OneDrive\Desktop\Test_hdf5_to_tiff', False)
File "C:\Users\wayne\OneDrive\Desktop\Test_hdf5_to_tiff\hdf5_to_tiffs.py", line 51, in generate_tiff_files
with fabio.open(fn) as fabio_img:
File "C:\Users\wayne\AppData\Local\Programs\Python\Python310\lib\site-packages\fabio\openimage.py", line 180, in openimage
obj = obj.read(obj.filename, frame)
File "C:\Users\wayne\AppData\Local\Programs\Python\Python310\lib\site-packages\fabio\eigerimage.py", line 159, in read
self._data = self.dataset[0][self.currentframe,:,:]
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Users\wayne\AppData\Local\Programs\Python\Python310\lib\site-packages\h5py\_hl\dataset.py", line 741, in __getitem__
return self._fast_reader.read(args)
File "h5py\_selector.pyx", line 370, in h5py._selector.Reader.read
OSError: Can't read data (can't open directory)