1

I am setting up a computer vision project to detect and process GFP proteins. I keep getting errors about my file not being a Tiff Image and a Byte Error. I don't quite understand what they mean and haven't found anything about it online.

I have already made sure that the file path is correct and have tried changing the file into Tiff Format. Now on Finder, it says that it is a TIFF Image but still gives an error.

import tifffile
from colicoords import Data, Cell, CellPlot
import matplotlib.pyplot as plt


binary_img = tifffile.imread('organoid_images/gfp/cells1.tif')
data = Data()
data.add_data(binary_img, 'binary')
cell = Cell(data)
cell.optimize()
cp = CellPlot(cell)

plt.figure()
cp.imshow('flu_514', cmap='viridis', interpolation='nearest')
cp.plot_outline()
cp.plot_midline()
plt.show()

Error Message:

Traceback (most recent call last):
  File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 2236, in __init__
    byteorder = {b'II': '<', b'MM': '>'}[header[:2]]
KeyError: b'\x89P'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "gfp.py", line 6, in <module>
    binary_img = tifffile.imread('organoid_images/gfp/cells1.tif')
  File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 715, in imread
    with TiffFile(files, **kwargs_file) as tif:
  File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 2238, in __init__
    raise TiffFileError('not a TIFF file')
tifffile.tifffile.TiffFileError: not a TIFF file
CosmoCrash
  • 61
  • 1
  • 7
  • 1
    As @MarkSetchell answered, it appears that your file is actually a PNG file; the bytes `\x89P` (i.e. `\x89\x50`) are the first two bytes in the [PNG signature](https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header). – Warren Weckesser Aug 01 '19 at 19:13
  • @WarrenWeckesser Thank you for your suggestion for improvement. I have edited it into my answer. – Mark Setchell Aug 01 '19 at 19:28

1 Answers1

2

Your file starting \x89P is a PNG file, not TIFF, as that is a PNG signature, whereas TIFF files start II if in Intel order, or MM if in Motorola order.

If on Linux/macOS, try running:

file cells1.tif

See Wikipedia for description of PNG signature, as suggested by Warren.

See Wikipedia for description of TIFF header.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks. The program works now but it obtains another problem. I get a value error and can't find anything about it. Error: ValueError: TiffPage 0: cannot decompress LZW – CosmoCrash Aug 01 '19 at 20:52
  • Please update your question then to show your current code and also share your image. If you can't upload image to Stack Overflow, add a link to Dropbox or Google Drive or similar. – Mark Setchell Aug 02 '19 at 06:25
  • @Mark Setchell How to detect if tiff is valid or not? You mentioned tiff starts II How can I check that exactly? – YasserKhalil Mar 17 '22 at 17:49
  • @YasserKhalil If you are on Linux or macOS, use `xxd YOURIMAGE.TIF | more`. If on Windows, go to https://hexed.it and load your image there to look at the first few bytes. – Mark Setchell Mar 17 '22 at 17:58
  • Thanks a lot. Is there a python code that enables me to detect if tiff is valid? I tried these lines `try:` `image = cv2.imread(img_name)` `image = np.asarray(image,dtype = np.float64)` `except:` `print('INVALID TIFF')`. The code works fine if the tiff is valid but it still throws an error if it is invalid `imread_('Sample1.tiff'): can't read data: OpenCV(4.5.5)` – YasserKhalil Mar 17 '22 at 18:01
  • @YasserKhalil Please just ask a new question and share your troublesome TIFF. Also, have a look here https://stackoverflow.com/a/60661431/2836621 – Mark Setchell Mar 17 '22 at 19:09
  • I already have a question at this link https://stackoverflow.com/questions/71511742/ – YasserKhalil Mar 17 '22 at 20:21