Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three different types of output when reading an image with three different libraries in Python

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.

like image 765
PURNENDU MISHRA Avatar asked Jan 17 '18 13:01

PURNENDU MISHRA


People also ask

How do you read and write an image in Python?

In Python and OpenCV, you can read (load) and write (save) image files with cv2. imread() and cv2. imwrite() . Images are read as NumPy array ndarray .

What format is the image read in using OpenCV in Python?

As I mentioned earlier, OpenCV reads the image in BGR format by default.


1 Answers

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

like image 105
GPPK Avatar answered Sep 30 '22 20:09

GPPK