如何在 cv2.imread PyQT4 中使用 Qstring
How to use Qstring for cv2.imread PyQT4
我是 python 的新人。我正在构建我的第一个应用程序。我的问题可能很简单,但我确实需要帮助。我有 2 个标签和 2 个按钮。
第一个按钮调用函数browse
并将浏览图像文件并将图像设置在标签1上。然后,第二个按钮调用函数applyBrowsedImage
并从标签1中获取图像并将其设置为cv2.imread,这样就可以用于图像处理了。
但是程序显示错误:
TypeError: expected string or Unicode object, QString found
def browse(self, path):
filePath = QtGui.QFileDialog.getOpenFileName(self, 'a file','*.jpg')
self.path = filePath
fileHandle = open(filePath, 'r')
pixmap = QPixmap(filePath)
self.label1.setPixmap(pixmap)
def ApplyBrowsedImage(self):
##a = cv2.imread('image.jpg') it works here
a = cv2.imread(self.path) ##but does not work here
pixmap = QPixmap(a)
self.label2.setPixmap(pixmap)
print("not yet")
我愿意接受任何参考或方法。
提前致谢。
Qt 为诸如字符串之类的东西提供了一些它自己的类型,因为 Qt 是一个 C++ 库,显然这些东西在 C++ 中很难。使用 PyQt 时,您可以选择使用 Qt 字符串 (QString
) 或 Python 本机字符串。旧版本的 PyQt (PyQt4) 默认使用 QString
,而新版本 (PyQt5) 使用 Python 字符串。
我认为最好的选择是使用本机 Python 类型,这使您可以轻松地与其他也期望本机 Python 类型的库集成,而无需进行一大堆烦人的转换。
为此,在导入 PyQt 之前(因此将其放在启动的 python 脚本的顶部),添加以下代码:
# do this BEFORE importing PyQt for the first time
import sip
for name in ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]:
sip.setapi(name, 2)
# now you can import PyQt...
这确保您使用本机 Python 类型而不是上面列出的类型,并且您的错误将消失。
我是 python 的新人。我正在构建我的第一个应用程序。我的问题可能很简单,但我确实需要帮助。我有 2 个标签和 2 个按钮。
第一个按钮调用函数browse
并将浏览图像文件并将图像设置在标签1上。然后,第二个按钮调用函数applyBrowsedImage
并从标签1中获取图像并将其设置为cv2.imread,这样就可以用于图像处理了。
但是程序显示错误:
TypeError: expected string or Unicode object, QString found
def browse(self, path):
filePath = QtGui.QFileDialog.getOpenFileName(self, 'a file','*.jpg')
self.path = filePath
fileHandle = open(filePath, 'r')
pixmap = QPixmap(filePath)
self.label1.setPixmap(pixmap)
def ApplyBrowsedImage(self):
##a = cv2.imread('image.jpg') it works here
a = cv2.imread(self.path) ##but does not work here
pixmap = QPixmap(a)
self.label2.setPixmap(pixmap)
print("not yet")
我愿意接受任何参考或方法。
提前致谢。
Qt 为诸如字符串之类的东西提供了一些它自己的类型,因为 Qt 是一个 C++ 库,显然这些东西在 C++ 中很难。使用 PyQt 时,您可以选择使用 Qt 字符串 (QString
) 或 Python 本机字符串。旧版本的 PyQt (PyQt4) 默认使用 QString
,而新版本 (PyQt5) 使用 Python 字符串。
我认为最好的选择是使用本机 Python 类型,这使您可以轻松地与其他也期望本机 Python 类型的库集成,而无需进行一大堆烦人的转换。
为此,在导入 PyQt 之前(因此将其放在启动的 python 脚本的顶部),添加以下代码:
# do this BEFORE importing PyQt for the first time
import sip
for name in ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]:
sip.setapi(name, 2)
# now you can import PyQt...
这确保您使用本机 Python 类型而不是上面列出的类型,并且您的错误将消失。