将 numpy 数组转换为 Pixmap 时程序崩溃错误
Program Crashes ERROR when converting a numpy array to Pixmap
**
版本信息:
- Python: 3.7.9 - 蟒蛇环境
- numpy = 1.19.4
- Pyqt5
**
我正在开发一个应用程序,我在其中加载二进制 (.bin) 文件并将其转换为二维 numpy 数组。我可以读取二进制文件,并且还设法将其转换为 numpy 数组。
变量的名称是 out,它的形状是 (512, 1536)。由于阵列的形状是二维的,它是一个灰度图像。变量包含:
out = array([[ 0, 107, 68, ..., 0, 2, 3],
[ 47, 65, 66, ..., 0, 1, 0],
[ 27, 24, 34, ..., 2, 0, 1],
...,
[124, 127, 124, ..., 136, 140, 147],
[125, 129, 125, ..., 130, 134, 132],
[130, 126, 126, ..., 139, 134, 130]], dtype=uint8)
我用opencv来显示图像:
import cv2
cv2.imshow('out', out)
这有效,我可以在新的 window 中看到灰度图像。
但是,当我尝试使用以下代码将 numpy 数组转换为 Pixmap 时,程序崩溃但没有输出任何错误:
qImg = QPixmap(QImage(out.data, out.shape[1], out.shape[0], QImage.Format_Grayscale8))
print(qImg)
print(qImg) 行应该输出一个像素图对象,但它没有,只是崩溃了。
我还尝试了一些其他的东西:
从这个 () 问题开始,我已经厌倦了使用 qimage2ndarray
库将 numpy 数组转换为 QImage。转换为 QImage 不是问题。但是当创建 QPixmap
对象时 程序崩溃。
q = qimage2ndarray.array2qimage(out)
pix = QPixmap.fromImage(q) # After this line the program crashes
完整脚本:
import numpy as np
import sys
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel
import matplotlib.pyplot as plt
import qimage2ndarray
var = np.array(np.loadtxt('out.txt')).astype(np.uint8)
new_var = var.copy()
q = qimage2ndarray.array2qimage(new_var)
pix = QPixmap.fromImage(q) # Program crashes here
app = QApplication(sys.argv)
w = QLabel()
w.setPixmap(pixmap)
w.show()
sys.exit(app.exec_())
这里的 'out.txt' 文件是一个包含 numpy 数组的文本文件。
这可能是什么原因?
感谢您的帮助。
谢谢。
编辑 1:
根据这个 link() 我还尝试了以下(脚本的扩展):
GRAY_COLORTABLE = [qRgb(i, i, i) for i in range(256)]
img_array = out.copy()
bytesPerLine = img_array.strides[1]
print(f'strides:{img_array.strides[1]}') # Output: 1
height, width = img_array.shape
image = QImage(img_array.data,
width, height,
bytesPerLine,
QImage.Format_Indexed8)
image.setColorTable(GRAY_COLORTABLE)
print('image: ', image)
print('test')
new = image.copy()
pixmap = QPixmap.fromImage(new) # Program crashes here
print(pixmap)
而且我仍然无法打印像素图对象。无法理解它。
感谢您的帮助。如果还需要其他东西,请告诉我。
如果有人遇到这个,我犯了一个愚蠢的错误。
为了使用QPixmap,应该首先创建QApplication。需要交换以下行。
app = QApplication(sys.argv)
pix = QPixmap.fromImage(q)
** 版本信息:
- Python: 3.7.9 - 蟒蛇环境
- numpy = 1.19.4
- Pyqt5 **
我正在开发一个应用程序,我在其中加载二进制 (.bin) 文件并将其转换为二维 numpy 数组。我可以读取二进制文件,并且还设法将其转换为 numpy 数组。 变量的名称是 out,它的形状是 (512, 1536)。由于阵列的形状是二维的,它是一个灰度图像。变量包含:
out = array([[ 0, 107, 68, ..., 0, 2, 3],
[ 47, 65, 66, ..., 0, 1, 0],
[ 27, 24, 34, ..., 2, 0, 1],
...,
[124, 127, 124, ..., 136, 140, 147],
[125, 129, 125, ..., 130, 134, 132],
[130, 126, 126, ..., 139, 134, 130]], dtype=uint8)
我用opencv来显示图像:
import cv2
cv2.imshow('out', out)
这有效,我可以在新的 window 中看到灰度图像。
但是,当我尝试使用以下代码将 numpy 数组转换为 Pixmap 时,程序崩溃但没有输出任何错误:
qImg = QPixmap(QImage(out.data, out.shape[1], out.shape[0], QImage.Format_Grayscale8))
print(qImg)
print(qImg) 行应该输出一个像素图对象,但它没有,只是崩溃了。
我还尝试了一些其他的东西:
从这个 (qimage2ndarray
库将 numpy 数组转换为 QImage。转换为 QImage 不是问题。但是当创建 QPixmap
对象时 程序崩溃。
q = qimage2ndarray.array2qimage(out)
pix = QPixmap.fromImage(q) # After this line the program crashes
完整脚本:
import numpy as np
import sys
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel
import matplotlib.pyplot as plt
import qimage2ndarray
var = np.array(np.loadtxt('out.txt')).astype(np.uint8)
new_var = var.copy()
q = qimage2ndarray.array2qimage(new_var)
pix = QPixmap.fromImage(q) # Program crashes here
app = QApplication(sys.argv)
w = QLabel()
w.setPixmap(pixmap)
w.show()
sys.exit(app.exec_())
这里的 'out.txt' 文件是一个包含 numpy 数组的文本文件。
这可能是什么原因?
感谢您的帮助。
谢谢。
编辑 1:
根据这个 link(
GRAY_COLORTABLE = [qRgb(i, i, i) for i in range(256)]
img_array = out.copy()
bytesPerLine = img_array.strides[1]
print(f'strides:{img_array.strides[1]}') # Output: 1
height, width = img_array.shape
image = QImage(img_array.data,
width, height,
bytesPerLine,
QImage.Format_Indexed8)
image.setColorTable(GRAY_COLORTABLE)
print('image: ', image)
print('test')
new = image.copy()
pixmap = QPixmap.fromImage(new) # Program crashes here
print(pixmap)
而且我仍然无法打印像素图对象。无法理解它。
感谢您的帮助。如果还需要其他东西,请告诉我。
如果有人遇到这个,我犯了一个愚蠢的错误。 为了使用QPixmap,应该首先创建QApplication。需要交换以下行。
app = QApplication(sys.argv)
pix = QPixmap.fromImage(q)