关于python:Image.open给出错误无法识别图像文件 您所在的位置:网站首页 newimage/wfish25.gif 关于python:Image.open给出错误无法识别图像文件

关于python:Image.open给出错误无法识别图像文件

2023-01-22 03:30| 来源: 网络整理| 查看: 265

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

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

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

1Import Image

代替

1From PIL import Image

我认为这是过时的解决方案; 我无法导入图片。 另一种解决方案是将枕头更新为2.9,但是5年后,我们将其设置为5.0.0。 我在收到相同错误的地方也尝试了4.0.0。 是否有最新解决方案?

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

1234image_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,其行为都相同。 Id说,坚持使用GDAL或转换为较低的每样本位数(最多16个)格式。 如果您有兴趣,这是调试输出:gist.github.com/mjpieters/522f47fa0466d8520ee835bf4006aac4

您有一个多通道32位TIFF图像,而Pillow尚不支持该格式。请参阅问题#1888:

Pillow (and PIL) is currently able to open 8 bit per channel multi-channel images (such as RGB) but is able to open higher bit depth images (e.g. I16, I32, or Float32 images) if they are single channel (e.g., grayscale).

[...]

Requirements

We should be able to support common GIS formats as well as high bit depth RGB(A) images. At least 4 channels, but potentially more (see #1839) Different pixel formats, including I16, I32, and Float.

我是通过直接使用TIFF插件图像读取器并启用调试模式来确定的:

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

其中包括输出:

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

(下面的完整调试输出)

您可以使用Python GDAL绑定读取此格式。您还可以使用gdal_translate命令行实用程序将文件转换为Pillow可以处理的格式;对于多频段,您必须降低到8位,或移动到灰度。

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

1gdal_translate -of PNG rgb_CGI.tiff rgb_CGI.png

之后,Pillow可以打开PNG文件。

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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970>>> 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: 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: 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: tag: GeoKeyDirectoryTag (34735) - type: short (3) Tag Location: 190 - Data Location: 17006 - value: tag: GeoDoubleParamsTag (34736) - type: double (12) Tag Location: 202 - Data Location: 17078 - value: 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: 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: 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: tag: GeoKeyDirectoryTag (34735) - type: short (3) Tag Location: 190 - Data Location: 17006 - value: tag: GeoDoubleParamsTag (34736) - type: double (12) Tag Location: 202 - Data Location: 17078 - value: 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"", line 1, in   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

我将tiff图像文件从64位格式转换为32位格式,并且可以正常工作。

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

1magick oldimage.tiff -depth 8 newimage.tif

或使用ImageMagick 6或更早版本:

1convert out_0.tiff -depth 8 newimage.tiff

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

123456789101112131415$ 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图像处理,并且比PIL快很多。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有