【问题标题】:"cannot identify image file"-error when trying to load a tiff in python“无法识别图像文件”-尝试在 python 中加载 tiff 时出错
【发布时间】:2020-12-17 20:02:45
【问题描述】:

我正在尝试访问存储在 tiff 文件中的温度数据。我得到了一个应该能够执行此操作的 python 脚本,但我不断收到以下错误:

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'

这是代码的相关部分:

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)

这是我检查的:

  • 与另一个随机 tiff 文件一起工作,所以它可能不是脚本,而是文件本身(/也许我需要安装一些额外的包??)
  • 可以在 IrfanView 和 Photoscan 中毫无问题地打开 tiff 文件
  • 当提示“文件 Testbild.tiff”时,我得到“Testbild.tiff:TIFF 图像数据,little-endian”,所以它肯定是一个 tiff 文件

有人能帮帮我吗?

干杯!

编辑: Image 的导入语句为from PIL import Image

如果需要,这些都是脚本的import语句:

import matplotlib.pyplot as plt
from PIL import Image
import sys
import numpy as np
import math
import cv2

【问题讨论】:

  • 能否包含Image 的导入语句。那里有不止一种叫做“图像”的东西。见stackoverflow.com/questions/19230991/…
  • 谢谢,我已经编辑了这个问题。我检查了您提供的链接,不幸的是那里没有给我答案(例如,我使用的导入语句是推荐的,我的 PIL 是最新的):-/
  • 在互联网上搜索PIL.UnidentifiedImageError: cannot identify image file 会得到很多点击 - 大多数会是死胡同,但那里有一些有趣的东西。特别是,在pillow TIFF documentation 中,您可能需要libtiff(以及底层C 库)。
  • 对于初学者(真的,完全),有时很难将死胡同与有前途的路线区分开来;-) 我当然已经花了一些时间在谷歌上搜索,并将继续这样做,会明天试试 libtiff(现在是睡觉时间),谢谢!
  • 也许你可以分享你的图片 - 如果不是直接在 Stack Overflow 上,可以使用 Google Drive 或 Dropbox。

标签: python image image-processing tiff


【解决方案1】:

尝试使用:

from skimage import io

im = io.imread(sourcePath)

这也会直接将其作为数组打开。

【讨论】:

    【解决方案2】:

    就我而言,它可以将.tif 文件读取到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)
    

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 2013-02-23
      • 2020-12-05
      • 2022-01-23
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多