7

I am reading an image in python with three different libraries

  1. imageio
  2. PIL.Image
  3. cv2.

The output I am getting on reading image with each one of these libraries is different. For example

  1. On reading with imageio

    a = imageio.imread('test_img.png')

    The output is of type - uint8 and size is (500,334,4)

  2. using Image

    b = Image.open('test_img.png')

    type - Image, size (334,500)

  3. using cv2

    c = cv2.imread('test_img.png')

    type- uint8, size (500,334,3)

Why I am getting three different size for same image when using three different libraries? Please, help me in understanding the difference.

PURNENDU MISHRA
  • 423
  • 5
  • 13
  • 3
    Do you understand what the numbers mean? – user202729 Jan 17 '18 at 13:58
  • 1
    all the sizes are the same... height 500, width 334. They may represent the data differently... the 3thrd dimension is the depth, in OpenCV, for example this means the channels which by default is 3 (BGR) when loading an image – api55 Jan 17 '18 at 13:59
  • Yes, I understand that I am getting height and width of image in each of these cases, but the number of channels in case of imageio is 4, where in case of Opencv it is 3. That is the reason of confusion for me. – PURNENDU MISHRA Jan 19 '18 at 13:42
  • 1
    I don’t really understand what you are asking: You are using three *different* libraries, so of course you cannot expect all three libraries to give you the same output. You should look up in the documentation of each library what each value means. – poke Feb 03 '18 at 13:32
  • @poke I was trying to read an indexed image and was not sure which library will be suitable. So I tried three different libraries, which gave me three different results for the same indexed image and also showed different image shapes. That was my reason for confusion. Also earlier I had no knowledge about the alpha channel. – PURNENDU MISHRA Feb 07 '18 at 07:00

1 Answers1

11

What you get returned from both imageio & OpenCV are three properties of the image, Height, Width & Channels (or Depth). For standard BGR images you only have 3 channels, that's why you see 3 for the OpenCV

For the imageio it is likely that it is reading a fourth channel, usually alpha, which represents the image transparency and is often found in PNG images.

If you wanted the fourth channel with OpenCV then you would need to use the following code:

Mat image = imread("image.png", IMREAD_UNCHANGED);

Which would give you a fourth channel

Vinay Lodha
  • 2,185
  • 20
  • 29
GPPK
  • 6,546
  • 4
  • 32
  • 57