geovolc
geovolc

Reputation: 1

"cannot identify image file"-error when trying to load a tiff in python

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:

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

Upvotes: 0

Views: 2659

Answers (2)

Sveta Grechin
Sveta Grechin

Reputation: 1

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)

Upvotes: 0

Avi Olan
Avi Olan

Reputation: 71

Try using:

from skimage import io

im = io.imread(sourcePath)

This will open it directly as an array as well.

Upvotes: 1

Related Questions