[英]Reproduce Python 2 PyQt4 QImage constructor behavior in Python 3
我使用PyQt4编写了一个小的GUI,该GUI显示图像并获取用户单击的点坐标。 我需要将2D numpy数组显示为灰度,因此我要从该数组创建一个QImage,然后从该数组创建一个QPixmap。 在Python 2中,它工作正常。
但是,当我转向Python 3时,它无法决定QImage的构造函数-它给了我以下错误:
TypeError: arguments did not match any overloaded call:
QImage(): too many arguments
QImage(QSize, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(list-of-str): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, str format=None): argument 1 has unexpected type 'numpy.ndarray'
QImage(QImage): argument 1 has unexpected type 'numpy.ndarray'
QImage(object): too many arguments
据我所知,我之前调用的QImage构造函数就是其中之一:
QImage(str, int, int, QImage.Format)
QImage(sip.voidptr, int, int, QImage.Format)
我假设一个numpy数组适合其中之一所需的协议之一。 我在想它可能与数组而不是视图有关,但是我尝试过的所有变化都会产生上述错误,或者只是使GUI退出而无所事事。 如何在Python 3中重现Python 2的行为?
以下是一个小示例,其中相同的确切代码在Python 2下可以正常工作,但在Python 3下却不能正常工作:
from __future__ import (print_function, division)
from PyQt4 import QtGui, QtCore
import numpy as np
class MouseView(QtGui.QGraphicsView):
mouseclick = QtCore.pyqtSignal(tuple)
def __init__(self, scene, parent=None):
super(MouseView, self).__init__(scene, parent=parent)
def mousePressEvent(self, event):
self.mouseclick.emit((event.x(),
self.scene().sceneRect().height() - event.y()))
class SimplePicker(QtGui.QDialog):
def __init__(self, data, parent=None):
super(SimplePicker, self).__init__(parent)
mind = data.min()
maxd = data.max()
bdata = ((data - mind) / (maxd - mind) * 255.).astype(np.uint8)
wdims = data.shape
wid = wdims[0]
hgt = wdims[1]
# This is the line of interest - it works fine under Python 2, but not Python 3
img = QtGui.QImage(bdata.T, wid, hgt,
QtGui.QImage.Format_Indexed8)
self.scene = QtGui.QGraphicsScene(0, 0, wid, hgt)
self.px = self.scene.addPixmap(QtGui.QPixmap.fromImage(img))
view = MouseView(self.scene)
view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
view.setSizePolicy(QtGui.QSizePolicy.Fixed,
QtGui.QSizePolicy.Fixed)
view.setMinimumSize(wid, hgt)
view.mouseclick.connect(self.click_point)
quitb = QtGui.QPushButton("Done")
quitb.clicked.connect(self.quit)
lay = QtGui.QVBoxLayout()
lay.addWidget(view)
lay.addWidget(quitb)
self.setLayout(lay)
self.points = []
def click_point(self, xy):
self.points.append(xy)
def quit(self):
self.accept()
def test_picker():
x, y = np.mgrid[0:100, 0:100]
img = x * y
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(['python'])
picker = SimplePicker(img)
picker.show()
app.exec_()
print(picker.points)
if __name__ == "__main__":
test_picker()
我在Windows 7 64位,Qt 4.8.7,PyQt 4.10.4,numpy 1.9.2上使用Anaconda安装。
在上面的PyQt构造函数中,从名为bdata
的Numpy数组中观察到以下行为:
bdata
对于Python 2和Python 3均正常工作 bdata.T
适用于2,而不适用于3(构造函数错误) bdata.T.copy()
对两个都适用 bdata[::-1,:]
不适用于2或3(相同的错误) bdata[::-1,:].copy()
都适用 bdata[::-1,:].base
两者均可使用,但会丢失反向操作的结果 正如@ekhumoro在评论中提到的那样,您需要一些支持Python 缓冲区协议的东西。 这里感兴趣的实际Qt构造函数是此 QImage构造函数或它的const版本:
QImage(uchar * data, int width, int height, Format format)
根据此处保存的PyQt 4.10.4文档,PyQt对unsigned char *
期望在Python 2和3中有所不同:
Python 2:
如果Qt期望使用
char *
,有signed char *
或unsigned char *
(或const版本),则PyQt4将接受unicode或QString,其中仅包含ASCII字符,str,QByteArray或实现缓冲协议的Python对象 。
Python 3:
如果Qt期望带
signed char *
或unsigned char *
(或const版本),则PyQt4将接受一个bytes 。
Numpy 数组满足这两个条件,但显然Numpy 视图都不满足。 实际上令人困惑的是bdata.T
在Python 2中bdata.T
工作,因为它据称返回了一个视图:
>>> a = np.ones((2,3))
>>> b = a.T
>>> b.base is a
True
最终答案:如果需要进行转换以生成视图,则可以通过将结果的copy()
到新数组以传递到构造函数中来避免错误。 这可能不是最佳答案,但它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.