python pixels <= CV_IO_MAX_IMAGE_PIXELS in function ‘cv::validateInputImageSize‘ 解决方法

python 在读取大的图像的时候出现 

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:79: error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'cv::validateInputImageSize'
错误。

import cv2
img_path = r"E:\data\map\2307041910\19\2307041910.tif"  # 图像大小 54528*67840
img = cv2.imread(img_path)
print(img.shape)
Traceback (most recent call last):
  File "D:/workspace/python/real_work/test.py", line 6, in 
    img = cv2.imread(img_path)
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:79: error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'cv::validateInputImageSize'

原因:By default number of pixels must be less than 2^30.

解决方法: Limit can be set using system variable OPENCV_IO_MAX_IMAGE_PIXELS

具体实现: python 程序前面增加如下两句

import os
os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = pow(2, 40).__str__()
import os
os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = pow(2, 40).__str__()

import cv2
img_path = r"E:\data\map\2307041910\19\2307041910.tif"  # 图像大小 54528*67840
img = cv2.imread(img_path)
print(img.shape)
(54528, 67840, 3)

Process finished with exit code 0

参考:

Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'validateInputImageSize' | Giving me headaches · Issue #15829 · opencv/opencv · GitHub

你可能感兴趣的:(opencv,python,开发语言)