Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

image - Reading MetaMorph stk file failed with PIL and tifffile

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...