【问题标题】:Image.open gives error Cannot identify image fileImage.open 给出错误无法识别图像文件
【发布时间】:2018-02-23 09:33:38
【问题描述】:

我正在尝试使用 PIL 的图像功能打开一个 geotiff 文件。它引发了错误:

OSError: cannot identify image file 'Whatever\\image\\I\\use.tiff'

我看到了here 提出的问题,例如,解决方案是使用

Import Image 

而不是

From PIL import Image

我认为这是一个过时的解决方案;我无法导入图像。另一个解决方案是将枕头更新到 2.9,但 5 年后我们使用的是 5.0.0。我也尝试了 4.0.0,我收到相同的错误。 有最新的解决方案吗?

这是我的代码,here 是一个文件的链接:

image_path = 'each\\image\\I\\use.tiff'

from PIL import Image
Image.open(image_path)

【问题讨论】:

  • 不,import Image 不是问题;在遥远的过去,PIL 有打包问题并且不会安装正确的包(所以PIL.Image 被安装为Image)。
  • 如果文件不能被 PIL / Pillow 打开,那么它可能不是受支持的格式,或者已损坏。试试pypi.python.org/pypi/GDAL?
  • 另一种可能是这个文件使用了压缩,这需要你安装 libtiff,见pillow.readthedocs.io/en/stable/handbook/…。无论如何,如果没有要测试的实际文件,我们无能为力。
  • 我添加了一个文件。我可以使用 gdal 和 qgis 打开文件,但 PIL 会引发错误。这只是一个示例,它对我使用的每个 TIFF 的行为都是相同的。
  • 坚持使用 GDAL 或转换为较低的每样本位数(最多 16 位)格式,我会说。

标签: python image pillow


【解决方案1】:

您有一个多通道 32 位 TIFF 图像,而 Pillow 尚不支持该格式。见issue #1888

Pillow(和 PIL)目前能够打开每通道 8 位的多通道图像(例如 RGB),但如果它们是单通道,则能够打开更高位深度的图像(例如 I16、I32 或 Float32 图像) (例如,灰度)。

[...]

要求

  • 我们应该能够支持常见的 GIS 格式以及高位深度 RGB(A) 图像。
  • 至少 4 个通道,但可能更多(参见 #1839)
  • 不同的像素格式,包括 I16、I32 和 Float。

我通过直接使用 TIFF 插件图像阅读器确定了这一点,并启用了调试模式:

from PIL import TiffImagePlugin
TiffImagePlugin.DEBUG = True
with open(image_path, 'rb') as f:
    TiffImagePlugin.TiffImageFile(f)

其中包括输出:

tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 218 - value: (32, 32, 32)

(下面的完整调试输出)

您可以使用Python GDAL bindings 阅读此格式。您还可以使用gdal_translate command line utility 将您的文件转换为 Pillow 可以 处理的格式;对于多波段,您必须降低到 8 位,或者转移到灰度。

例如,要将输入文件转换为 PNG,您可以使用:

gdal_translate -of PNG rgb_CGI.tiff rgb_CGI.png

之后 Pillow 可以打开 PNG 文件。


来自 Pillow 的 TIFF 插件的完整调试输出:

>>> from PIL import TiffImagePlugin
>>> TiffImagePlugin.DEBUG = True
>>> with open(image_path, 'rb') as f:
...     TiffImagePlugin.TiffImageFile(f)
...
*** TiffImageFile._open ***
- __first: 8
- ifh:  b'II*\x00\x08\x00\x00\x00'
Seeking to frame 0, on frame -1, __next 8, location: 8
Loading tags, location: 8
tag: ImageWidth (256) - type: short (3) - value: 2924
tag: ImageLength (257) - type: short (3) - value: 2088
tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 218 - value: (32, 32, 32)
tag: Compression (259) - type: short (3) - value: 1
tag: PhotometricInterpretation (262) - type: short (3) - value: 1
tag: StripOffsets (273) - type: long (4) Tag Location: 82 - Data Location: 8576 - value: <table: 8352 bytes>
tag: SamplesPerPixel (277) - type: short (3) - value: 3
tag: RowsPerStrip (278) - type: short (3) - value: 1
tag: StripByteCounts (279) - type: long (4) Tag Location: 118 - Data Location: 224 - value: <table: 8352 bytes>
tag: PlanarConfiguration (284) - type: short (3) - value: 1
tag: ExtraSamples (338) - type: short (3) - value: (0, 0)
tag: SampleFormat (339) - type: short (3) Tag Location: 154 - Data Location: 16928 - value: (2, 2, 2)
tag: ModelPixelScaleTag (33550) - type: double (12) Tag Location: 166 - Data Location: 16934 - value: (0.25, 0.25, 0.0)
tag: ModelTiepointTag (33922) - type: double (12) Tag Location: 178 - Data Location: 16958 - value: <table: 48 bytes>
tag: GeoKeyDirectoryTag (34735) - type: short (3) Tag Location: 190 - Data Location: 17006 - value: <table: 72 bytes>
tag: GeoDoubleParamsTag (34736) - type: double (12) Tag Location: 202 - Data Location: 17078 - value: <table: 56 bytes>
tag: GeoAsciiParamsTag (34737) - type: string (2) Tag Location: 214 - Data Location: 17134 - value: Amersfoort / RD New|Amersfoort|
tag: ImageWidth (256) - type: short (3) - value: 2924
tag: ImageLength (257) - type: short (3) - value: 2088
tag: BitsPerSample (258) - type: short (3) Tag Location: 46 - Data Location: 218 - value: (32, 32, 32)
tag: Compression (259) - type: short (3) - value: 1
tag: PhotometricInterpretation (262) - type: short (3) - value: 1
tag: StripOffsets (273) - type: long (4) Tag Location: 82 - Data Location: 8576 - value: <table: 8352 bytes>
tag: SamplesPerPixel (277) - type: short (3) - value: 3
tag: RowsPerStrip (278) - type: short (3) - value: 1
tag: StripByteCounts (279) - type: long (4) Tag Location: 118 - Data Location: 224 - value: <table: 8352 bytes>
tag: PlanarConfiguration (284) - type: short (3) - value: 1
tag: ExtraSamples (338) - type: short (3) - value: (0, 0)
tag: SampleFormat (339) - type: short (3) Tag Location: 154 - Data Location: 16928 - value: (2, 2, 2)
tag: ModelPixelScaleTag (33550) - type: double (12) Tag Location: 166 - Data Location: 16934 - value: (0.25, 0.25, 0.0)
tag: ModelTiepointTag (33922) - type: double (12) Tag Location: 178 - Data Location: 16958 - value: <table: 48 bytes>
tag: GeoKeyDirectoryTag (34735) - type: short (3) Tag Location: 190 - Data Location: 17006 - value: <table: 72 bytes>
tag: GeoDoubleParamsTag (34736) - type: double (12) Tag Location: 202 - Data Location: 17078 - value: <table: 56 bytes>
tag: GeoAsciiParamsTag (34737) - type: string (2) Tag Location: 214 - Data Location: 17134 - value: Amersfoort / RD New|Amersfoort|
*** Summary ***
- compression: raw
- photometric_interpretation: 1
- planar_configuration: 1
- fill_order: 1
- size: (2924, 2088)
format key: (b'II', 1, (2, 2, 2), 1, (32, 32, 32), (0, 0))
- unsupported format
Traceback (most recent call last):
  File "/Users/mjpieters/Development/venvs/stackoverflow-3.6/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1196, in _setup
    self.mode, rawmode = OPEN_INFO[key]
KeyError: (b'II', 1, (2, 2, 2), 1, (32, 32, 32), (0, 0))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/venvs/stackoverflow-3.6/lib/python3.6/site-packages/PIL/ImageFile.py", line 102, in __init__
    self._open()
  File "/Users/mjpieters/Development/venvs/stackoverflow-3.6/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 950, in _open
    self._seek(0)
  File "/Users/mjpieters/Development/venvs/stackoverflow-3.6/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1017, in _seek
    self._setup()
  File "/Users/mjpieters/Development/venvs/stackoverflow-3.6/lib/python3.6/site-packages/PIL/TiffImagePlugin.py", line 1200, in _setup
    raise SyntaxError("unknown pixel mode")
SyntaxError: unknown pixel mode

【讨论】:

  • 我遇到了类似的问题。我做了一点实验,在尝试打开 CMYK/16 位图像时出现问题。它不会给出 CMYK/8、RGB/8 或 RGB/16 的错误。你能详细说明如何安装和使用GDAL吗?它似乎比枕头更复杂。
  • 我已经发布了一个关于 GDAL 安装的问题,因为它看起来很混乱。如果您能看一下,我将不胜感激:stackoverflow.com/q/50721107/2202732
【解决方案2】:

我将 tiff 图像文件从 64 位格式转换为 32 位格式,并且成功了。

您可以使用 ImageMagick 7 做到这一点:

magick oldimage.tiff -depth 8 newimage.tif

或使用 ImageMagick 6 或更早版本:

convert out_0.tiff -depth 8 newimage.tiff

【讨论】:

    【解决方案3】:

    pyvips 可以像这样直接使用 TIFF。它将您的文件加载为具有两个 Alpha 通道的单声道 int32 图像。

    $ python3
    Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pyvips
    >>> x = pyvips.Image.new_from_file("rgb_CGI.tiff")
    >>> x.width, x.height, x.bands, x.format, x.interpretation
    (2924, 2088, 3, 'int', 'b-w')
    >>> # getpoint reads out a pixel
    >>> x.getpoint(10, 10)
    [156.0, 141.0, 133.0]
    >>> # avg finds the image average
    >>> x.avg()
    125.31475912560515
    >>> 
    

    它忽略了额外的GDAL标签,但你可以做所有通常的2D图像处理,它是quite a bit quicker than PIL

    【讨论】:

    • 仅供参考 pyvips 不是标准模块。您需要下载 Windows 二进制文件并将 bin 文件夹添加到您的路径中。但它可以工作(PIL 和 tifffile 无法加载和保存我的 TIF 文件)。
    猜你喜欢
    • 2013-10-14
    • 2016-09-03
    • 2021-05-19
    • 2018-05-01
    • 1970-01-01
    • 2021-01-13
    • 2022-01-01
    • 2020-05-26
    • 2017-09-11
    相关资源
    最近更新 更多