1. 直接将ndarray彩色图像转化为 QImage 对象:
#解决中文路径下cv2无法读取的问题#path为文件路径colorimg = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_COLOR)imgshow = cv2.cvtColor(colorimg, cv2.COLOR_BGR2RGB)#彩色height, width, bytesPerComponent = imgshow.shapebytesPerLine = bytesPerComponent * width #表示彩色图像每个像素占用3个(ndarray图像数组的第三维长度)字节的空间QImg = QImage(imgshow.data, width, height, bytesPerLine, QImage.Format_RGB888)
2. 先将彩色图片转化为灰度图像,再转化为QImage对象:
#解决中文路径下cv2无法读取的问题colorimg = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_COLOR)img = cv2.cvtColor(colorimg, cv2.COLOR_BGR2GRAY)#灰度bytesPerLine = width #表示彩色图像每个像素占用1个字节的空间QImg = QImage(img.data, width, height,width, QImage.Format_Indexed8)