How to install OpenCV_python to read JPEG2000 files?
Is Jasper still the recommended way to read and write JP2 files in OpenCV? I heard there were security issues with it and OpenCV is moving to OpenJPEG. Has that happened yet?
I tried to use OpenCV with Jasper by enabling it in Python code as follows:
os.environ['OPENCV_IO_ENABLE_JASPER'] = 'true'
However, when I attempt to read an ESA Sentinel-2 image like this...
img_nir = cv2.imread(nir_path, 0)
I still get this error:
cv2.error: OpenCV(4.1.2) D:\Build\OpenCV\opencv-4.1.2\modules\imgcodecs\src\grfmt_jpeg2000.cpp:104: error: (-213:The function/feature is not implemented) imgcodecs: Jasper (JPEG-2000) codec is disabled. You can enable it via 'OPENCV_IO_ENABLE_JASPER' option. Refer for details and cautions here: https://github.com/opencv/opencv/issu... in function 'cv::initJasper'
I installed OpenCV in an anaconda Python 3.7 virtual environment on Windows 10 like this:
python -m pip install opencv_python-4.1.2-cp37-cp37m-win_amd64.whl
What's the current recommended way to read and write JP2 files in OpenCV_python?
Perhaps you should set the environment variable to 'on' instead of 'true'.
Thanks for your reply. However, it still fails with the same error message with it set to true and 1.
os.environ['OPENCV_IO_ENABLE_JASPER'] = 'true'
and
os.environ['OPENCV_IO_ENABLE_JASPER'] = '1'
Not sure if this is helpful in your scenario, but I got it to work by setting the environment variable in Powershell:
$Env:OPENCV_IO_ENABLE_JASPER = "True"
as mvuori said it needs to be set to "ON"!
I believe I was having the same issue as you. As explained in this GitHub comment, I needed to set the environment variable before even importing OpenCV. For example: