You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

skimage/imageio读取灰度PNG图片失败的问题求助

问题描述

此前长期使用skimage.io.imread或imageio.imread读取灰度图片正常,未修改代码的情况下突然读取失败,但合作者可正常读取目标文件。读取彩色图片无异常,已重装scikit-image、imageio、Pillow库仍无法解决。

图片属性信息

使用ImageMagick的identify命令查看图片属性:

identify test/resources/biosynth1_cropped/text_removed.png 
test/resources/biosynth1_cropped/text_removed.png PNG 1512x315 1512x315+0+0 8-bit sRGB 48c 10094B 0.000u 0:00.005

添加--verbose参数查看的部分属性:

Type: Grayscale
  Base type: Undefined
  Endianness: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
  Channel statistics:
    Pixels: 476280

测试代码

from skimage import io
import imageio
file="/Users/pm286/workspace/pyamiimage/test/resources/biosynth1_cropped/arrows_removed.png" 
img = imageio.imread(file)

报错信息

(base) pm286macbook:pyamiimage pm286$ python test_load.py 
Traceback (most recent call last):
  File "test_load.py", line 9, in <module>
    img = imageio.imread(file)
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/functions.py", line 265, in imread
    reader = read(uri, format, "i", **kwargs)
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/functions.py", line 186, in get_reader
    return format.get_reader(request)
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/format.py", line 170, in get_reader
    return self.Reader(self, request)
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/format.py", line 221, in __init__
    self._open(**self.request.kwargs.copy())
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 298, in _open
    return PillowFormat.Reader._open(self, pilmode=pilmode, as_gray=as_gray)
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 138, in _open
    as_gray=as_gray, is_gray=_palette_is_grayscale(self._im)
  File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 689, in _palette_is_grayscale
    palette = np.asarray(pil_image.getpalette()).reshape((256, 3))
ValueError: cannot reshape array of size 144 into shape (256,3)

解决方案建议

  • 强制指定灰度读取模式:在imread中添加as_gray=True参数,绕过imageio的调色板检测逻辑,直接生成单通道(x,y)维度的灰度数组,同时解决维度变为(x,y,3)的问题:

    # 使用imageio
    img = imageio.imread(file, as_gray=True)
    # 使用skimage
    img = io.imread(file, as_gray=True)
    
  • 手动处理调色板图片:直接用Pillow打开图片,手动转换为灰度模式后再转为numpy数组,避免依赖imageio的自动解析:

    from PIL import Image
    import numpy as np
    
    img_pil = Image.open(file)
    # 若为调色板模式,转为单通道灰度
    if img_pil.mode == 'P':
        img_pil = img_pil.convert('L')
    img = np.array(img_pil)
    
  • 对齐Pillow版本:检查你和合作者的Pillow版本是否一致,不同版本对调色板PNG的处理逻辑可能存在差异:

    import PIL
    print(PIL.__version__)
    

    若版本不同,尝试升级/降级到和合作者相同的版本。

  • 重新保存图片为标准灰度格式:用ImageMagick或Pillow将异常图片转换为标准单通道灰度PNG,从源头上解决格式问题:

    # ImageMagick命令
    convert test/resources/biosynth1_cropped/text_removed.png -type Grayscale -depth 8 fixed_gray.png
    

    或用Python代码转换:

    from PIL import Image
    img = Image.open(file)
    img.convert('L').save('fixed_gray.png')
    

内容的提问来源于stack exchange,提问作者peter.murray.rust

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 09:06:38