I'm reading an jp2 file using openCV's imread().
The image loads, but it's always grayscale.
I saw in the documentation that some IMREAD_FLAGS could cause this and I also know that cv2 may change the channel order to BGR.
However, I believe none of these reasons are the issue here. I imported the image with different IMREAD_FLAGS and saved copies like this:
image = cv2.imread("mypath.jp2", cv2.IMREAD_UNCHANGED) # change flag here
cv2.imwrite("IMREAD_UNCHANGED.png", image) # change file name accordingly
I also converted the image to RGB using cv2.cvtColor(image,cv2.COLOR_BGR2RGB). The resulting image sizes are different, but the result are always a grayscale image:
Also, I'm certain these images have color, because when opening it with Pillow it looks like this:
Can someone tell me what I'm doing wrong here? Thanks in advance !


When I use
cv.imread(path)(OpenCV 4.7.0 on Windows), it gives me three channels, not grayscale. I didn't look to check if OpenCV removed any color information.It does complain if I ask for
IMREAD_UNCHANGED:The same error happens for
cv.imreadmulti(path, flags=cv.IMREAD_UNCHANGED)which is usually the right choice for multi-plane image formats (TIFF can be like that). Without the flag, it just returns one layer of 3-channel data.So there's potential for a bug report. If you want to file it, do it on OpenCV's github.
PIL reads it as "RGBA". There is no transparency there, it's just a 4th channel.
The file name contains "rgbi", which makes me suspect it's a multi-layer file, or at least the fourth channel contains another color layer, not "transparency".
If you convert from PIL Image to numpy array, at least you can get all the channels and their data.
You could also use
imageio. It warns about the image being huge, but it does load the whole thing. It's a complex library, so check the docs for recommended ways to read such files.