I am reading an image in python with three different libraries
The output I am getting on reading image with each one of these libraries is different. For example
On reading with imageio
a = imageio.imread('test_img.png')
The output is of type - uint8 and size is (500,334,4)
using Image
b = Image.open('test_img.png')
type - Image, size (334,500)
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.
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 .
As I mentioned earlier, OpenCV reads the image in BGR format by default.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With