I am using PIL to read a MetaMorph stk file that contains a series of cryo-EM images.
Here is the code:
from PIL import Image
img = Image.open('particles_export.stk')
img
it returns <PIL.SpiderImagePlugin.SpiderImageFile image mode=F size=109x109 at 0x7F77085833C8>
The stk file should contain 50 images, but when I tried
img.seek(2)
, it returns EOFError: attempt to seek outside sequence, while the img page 0 and page 1 are two same page.
I've checked the stk file with ImageJ. It works fine, so the file is not corrupted.
I also tried the module tifffile with the code
import tifffile
stk = tifffile.imread('particles_export.stk')
it returns an error
KeyError Traceback (most recent call last)
/opt/anaconda3/envs/cryo/lib/python3.6/site-packages/tifffile/tifffile.py in __init__(self, arg, name, offset, size, _multifile, _useframes, _master, **kwargs)
2754 try:
-> 2755 byteorder = {b'II': '<', b'MM': '>', b'EP': '<'}[header[:2]]
2756 except KeyError:
KeyError: b'x00x00'
During handling of the above exception, another exception occurred:
TiffFileError Traceback (most recent call last)
<ipython-input-25-d5856ab518a1> in <module>()
4
5
----> 6 stk = tifffile.imread(filename)
7 #stk = imageio.mimread(filename, format = 'SPIDER' ,is_stk = True)
/opt/anaconda3/envs/cryo/lib/python3.6/site-packages/tifffile/tifffile.py in imread(files, aszarr, **kwargs)
710
711 if isinstance(files, (str, os.PathLike)) or hasattr(files, 'seek'):
--> 712 with TiffFile(files, **kwargs_file) as tif:
713 if aszarr:
714 return tif.aszarr(**kwargs)
/opt/anaconda3/envs/cryo/lib/python3.6/site-packages/tifffile/tifffile.py in __init__(self, arg, name, offset, size, _multifile, _useframes, _master, **kwargs)
2755 byteorder = {b'II': '<', b'MM': '>', b'EP': '<'}[header[:2]]
2756 except KeyError:
-> 2757 raise TiffFileError('not a TIFF file')
2758
2759 version = struct.unpack(byteorder + 'H', header[2:4])[0]
TiffFileError: not a TIFF file
I am wondering how can I read this file? I want to save the images as several single tiff files.
You may find this file here.
Thanks for any advice!
question from:
https://stackoverflow.com/questions/65897911/reading-metamorph-stk-file-failed-with-pil-and-tifffile