【问题标题】:Reshaping png image read by skimage.imread return ValueError重塑由 skimage.imread 读取的 png 图像返回 ValueError
【发布时间】:2021-11-08 00:13:58
【问题描述】:

在以下代码中使用 616x346 png 图像作为输入可以正常工作:

from skimage import io

image = io.imread('img.png')
image = image.reshape(image.shape[0] * image.shape[1], 3)

...但是如果我将图像尺寸更改为 640x451,则会出现错误

ValueError: 无法将大小为 1154560 的数组重塑为形状 (288640,3)

有什么想法吗?

【问题讨论】:

    标签: python python-3.x scikit-image


    【解决方案1】:

    您尝试重塑的 640x451 图像的形状是 (640, 451, 4) 而不是 (640, 451, 3)。这就是您无法将其转换为(640*451, 3) 的原因。查看image.shape 在 616x346 和 640x451 情况下的输出。一种解决方法是先将其从 rgba 转换为 rgb -

    from skimage import io, color
    image = io.imread('img.png')
    image = color.rgba2rgb(image)
    image = image.reshape(image.shape[0] * image.shape[1], 3)
    

    【讨论】:

    • 但是为什么图像只是通过改变像素尺寸来添加一个alpha通道呢?还是我理解错了?
    • 图像已经有 alpha 通道,没有你的更改 @user3607022
    • 但是初始 616x346 的 image.shape 产生 (616, 346, 3) - 这不意味着没有 alpha 通道吗?
    • 你说的是两个不同的图像吧?在我看来,第一张图应该是 RGB,第二张图应该是 RGBA。
    • 其实是同一张图。但是,当我在绘画中打开它并稍微更改尺寸并重新保存它时,绘画可能会使用 Alpha 通道保存它...
    猜你喜欢
    • 2019-09-01
    • 2018-02-17
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    相关资源
    最近更新 更多