繁体   English   中英

在 python 中使用 imageio.imread 打开图像

[英]Opening Image using imageio.imread in python

在python中,我使用style_image = imageio.imread('image.jpg')读取图像,然后打印图像的形状print(np.shape(style_image)) ,我发现大小为(216, 154, 4) ,为什么是 4 而不是 3 因为应该只有 3 种颜色。

您的图像可能有一个 alpha 通道来处理图像透明度,默认情况下imageio正在适应此图像并已使用附加通道读取它,而无需指定任何内容。 虽然,您的图像是 jpeg 图像,并且这种格式不存储 alpha 通道,所以如果您检查第四个通道,它可能充满了一个。

编辑:

我搜索了一下,惊讶地发现 jpeg 可以支持 alpha 通道。 但这并没有被广泛使用。 有关更多信息,请查看此问题 imageio支持这些类型的 jpeg,如您在文档页面中所见。

如果您像我一样偶然发现了这个问题,想知道是否有办法读取其中包含 alpha 通道的图像文件(就像 OP 一样)但没有 alpha 通道数据,答案是肯定的(至少使用 Pillow 后端时):

style_image = imageio.imread('image.jpg', pilmode='RBG')

从文档中,通过imageio.help('jpg')访问:

    Parameters for reading
    ----------------------
    exifrotate : bool
        Automatically rotate the image according to exif flag. Default True.
    pilmode : str
        From the Pillow documentation:
        
        * 'L' (8-bit pixels, grayscale)
        * 'P' (8-bit pixels, mapped to any other mode using a color palette)
        * 'RGB' (3x8-bit pixels, true color)
        * 'RGBA' (4x8-bit pixels, true color with transparency mask)
        * 'CMYK' (4x8-bit pixels, color separation)
        * 'YCbCr' (3x8-bit pixels, color video format)
        * 'I' (32-bit signed integer pixels)
        * 'F' (32-bit floating point pixels)
        
        PIL also provides limited support for a few special modes, including
        'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa'
        (true color with premultiplied alpha).
        
        When translating a color image to grayscale (mode 'L', 'I' or 'F'),
        the library uses the ITU-R 601-2 luma transform::
        
            L = R * 299/1000 + G * 587/1000 + B * 114/1000
    as_gray : bool
        If True, the image is converted using mode 'F'. When `mode` is
        not None and `as_gray` is True, the image is first converted
        according to `mode`, and the result is then "flattened" using
        mode 'F'.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM