0

I am trying to access to the temperature data stored in a tiff-file. I was provided with a python script that was supposed to be able to do this, but I keep getting the following error:

Traceback (most recent call last):
File "read_tiff.py", line 57, in <module>
im = Image.open(sourcePath)
File "/Users/myname/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py", line 2943, in open
raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file 'Testbild.tiff'

This is the relevant section of the code:

sourcePath = "Testbild.tiff"
im = []
try:
    sourcePath = sys.argv[1]
except IndexError:
    print('usage: python read_tiff.py filename')
    sys.exit()
try:
    im = Image.open(sourcePath)
except FileNotFoundError:
    print('File not found: ' + sourcePath)
    sys.exit()
imarray = np.array(im)

This is what I checked:

  • with another random tiff file it worked, so it is probably not the script but the file itself (/maybe I need to install some additional package??)
  • the tiff file can be opened without a problem in IrfanView and Photoscan
  • when prompting "file Testbild.tiff" I get "Testbild.tiff: TIFF image data, little-endian" so it definitely IS a tiff file

Is anyone able to help me out?

Cheers!

EDIT: The import statement for Image is from PIL import Image

If necessary, these are all the import statements of the script:

import matplotlib.pyplot as plt
from PIL import Image
import sys
import numpy as np
import math
import cv2
Kenly
  • 24,317
  • 7
  • 44
  • 60
geovolc
  • 1
  • 2
  • Can you include the import statement for `Image`. There is more than one thing called "Image" out there. See https://stackoverflow.com/questions/19230991/image-open-cannot-identify-image-file-python. – tdelaney Dec 17 '20 at 20:12
  • Thank you, I have edited the question. I have checked the link you provided, unfortunately there was no answer for me there (e.g. the import statement I used is the recommended one, and my PIL is up to date) :-/ – geovolc Dec 17 '20 at 21:00
  • An internet search of `PIL.UnidentifiedImageError: cannot identify image file` gives a lot of hits - most will be dead ends, but there is interesting stuff out there. In particular, from the [pillow TIFF documentation](https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#tiff) you may need `libtiff` (and the underlying C libraries). – tdelaney Dec 17 '20 at 21:08
  • For a beginner (really, completely) it is sometimes hard to distinguish the dead ends from the promising routes ;-) I have of course already spent some time googling this and will continue to do so, will try libtiff tomorrow (it's now bed time here), thanks! – geovolc Dec 17 '20 at 21:21
  • Maybe you could share your image - with Google Drive or Dropbox if not directly on Stack Overflow. – Mark Setchell Dec 17 '20 at 21:31
  • tiffinfo or indentify to try to figureout why is dfferent from the random one ? – pippo1980 Jan 22 '21 at 16:16

2 Answers2

1

Try using:

from skimage import io

im = io.imread(sourcePath)

This will open it directly as an array as well.

Avi Olan
  • 61
  • 1
  • 11
0

In my case, it worked to read .tif file to ndarray:

path2dir = r'C:\data\WORK\image4example'
name_img = 'Slice_25.tif'
path2img = os.path.join(path2dir, name_img)
im = cv2.imread(path2img , cv2.IMREAD_ANYDEPTH)
plt.imshow(im)
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22