0

In python, I use style_image = imageio.imread('image.jpg') to read an image, then I print the shape of the image print(np.shape(style_image)), I find that the size is (216, 154, 4), why it is 4 instead of 3 because there should be 3 colors only.

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
KHCheng
  • 621
  • 2
  • 6
  • 9

2 Answers2

1

Your image probably has an alpha channel to deal with image transparency and by default imageio is adapting to this image and reading it already with the additional channel without needing to specify anything. Although, your image is a jpeg image and this format doesn't store an alpha channel, so if you check the fourth channel it's probably filled with ones.

Edit:

I've searched a little and surprisingly discovered that jpeg can support alpha channel. But this isn't widely used. For further information, take a look at this question. And imageio supports these kinds of jpeg as you can see in this and in this documentation pages.

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
0

If you're like me and stumbled on this question wondering whether there's a way to read an image file with an alpha channel in it (as was the case with the OP) but without the alpha channel data, the answer is yes (at least when using the Pillow backend):

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

From the documentation, accessed via 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'.
FriendFX
  • 2,929
  • 1
  • 34
  • 63