- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
TL;DR:您只需要阅读“更新”部分。
How do you output
numpy.random.random((256, 256))
as a colormap to a qt scene?
这是摘要的摘要。
以下内容为我提供了我想要保存到文件的颜色图。
scaled_image = Image.fromarray(np.uint8(self.image*255))
plt.savefig("/home/test.png")
self.image
是 256x256 的 numpy 数组,它的所有值都在 -1 和 1 之间。
如何在 Qt 中将此图像输出到场景中?你可以使用 self.image = numpy.random.random((256, 256))
有一个和我类似的起点。如何将 2D numpy 随机值数组作为颜色图放到 pyqt 场景中?
如此接近。这似乎有效,但规模已经倒转。蓝色现在很热,红色很冷。如何切换它以使其看起来像上图?
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
gcf().canvas.draw() # IMPORTANT!
stringBuffer = gcf().canvas.buffer_rgba() # IMPORTANT!
l, b, w, h = gcf().bbox.bounds
qImage = QtGui.QImage(stringBuffer,
w,
h,
QtGui.QImage.Format_ARGB32)
pixmap = QtGui.QPixmap.fromImage(qImage)
pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmapItem)
self.graphicsView.setScene(scene)
下面给了我一个空白屏幕
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
pixMap = QPixmap(scaled_image)
scene.addPixmap(pixMap)
self.graphicsView.setScene(scene)
下面给我一个灰度输出到 Qt 场景。为什么不是彩色的?
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
imgQ = ImageQt(scaled_image)
pixMap = QtGui.QPixmap.fromImage(imgQ)
scene.addPixmap(pixMap)
self.graphicsView.setScene(scene)
我从颜色图问题的解决方案进行逆向工程 here并显示图像 here .
使用已发布解决方案的建议,我尝试先创建一个 QGraphicsPixmapItem
,然后将该项目添加到场景中。官方文档有点困惑,所以我终于从更脆的 pyside docs 中得到了我需要的东西.可悲的是,我得到了与上面相同的灰度。代码如下:
scene = QGraphicsScene(self)
scaled_image = Image.fromarray(np.uint8(self.image*255))
imgQ = ImageQt(scaled_image)
pixMap = QtGui.QPixmap.fromImage(imgQ)
pixMapItem = QtGui.QGraphicsPixmapItem(pixMap)
scene.addItem(pixMapItem)
self.graphicsView.setScene(scene)
self.image
是 numpy 二维数组,所有值都在 -1 和 1 之间。
我按如下缩放并得到灰度图像。但是,我想要一个颜色图:
scene = QGraphicsScene(self)
scaled_image = (self.image + 1) * (255 / 2)
scene.addPixmap(QPixmap.fromImage(qimage2ndarray.array2qimage(scaled_image)))
self.graphicsView.setScene(scene)
#min(self.image) = -0.64462
#max(self.image) = 1.0
如果我改为执行以下操作,我会得到我想要的颜色图,例如我之前开发的 Web 应用程序的下图中
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> imgplot = ax.imshow(self.image)
如何将颜色图添加到 qt 场景,而不仅仅是灰度?
最佳答案
QPixmap
支持大于 255 和 multiple image formats 的颜色值.如果不是这样,Qt 中的所有图标都会被灰度化(显然不是这样;))。
你可以用任何你想做的方式生成你的颜色图(在(使用 OpenCV 和 numpy)之前或在将其转换为 QImage
(使用 Qt)之后),将其转换为 QPixmap
并使用 将其附加到您的 QGraphicsPixmapItem
(不要直接将 QPixmap
用作 QGraphicScene
的一部分)QGraphicsScene
。
编辑:我误读了文档。您实际上可以通过使用 addPixmap()
在 QGraphicScene 中直接使用 QPixmap
。对于那个很抱歉。作为道歉,这里有一些代码给你。 :P
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class ImageView(QGraphicsView):
def __init__(self, pixmap=None, parent=None):
'''
QGraphicsView shows the image
'''
super(ImageView, self).__init__(parent)
self.pixmap = pixmap
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
layout = QVBoxLayout(self)
pic = QPixmap('/home/-----/Pictures/7 Cat Sins - Wrath.jpg')
grview = ImageView(pic, self)
layout.addWidget(grview)
self.setFixedSize(pic.size())
scene = QGraphicsScene(self)
scene.addPixmap(pic)
grview.setScene(scene)
self.show()
def main():
app = QApplication(sys.argv)
w = Widget()
return sys.exit(app.exec_())
if __name__ == '__main__':
main()
它产生以下结果:
图片来源:在 Google 上寻找 7 Cat Sins - Wrath ;)
基本上,您创建一个图像查看器,创建场景图,将像素图添加到场景图中,将图像查看器的场景设置为该图并使用该查看器在屏幕上显示像素图。
EDIT2:好的,看来您在实际集成 matplotlib
时遇到了问题。以下是您的操作方法(取自 here,对 gcf().canvas.buffer_rgba()
稍作改动):
import numpy as np
from matplotlib import use
use('AGG')
from matplotlib.pylab import *
from PySide import QtCore,QtGui
# Following are used for the generation of the image but you have your own imports so you don't need them
from matplotlib.transforms import Bbox
from matplotlib.path import Path
from matplotlib.patches import Rectangle
# Generation of the figure (you can skip all that and use your stuff)
rect = Rectangle((-1, -1), 2, 2, facecolor="#aaaaaa")
gca().add_patch(rect)
bbox = Bbox.from_bounds(-1, -1, 2, 2)
for i in range(12):
vertices = (np.random.random((4, 2)) - 0.5) * 6.0
vertices = np.ma.masked_array(vertices, [[False, False], [True, True], [False, False], [False, False]])
path = Path(vertices)
if path.intersects_bbox(bbox):
color = 'r'
else:
color = 'b'
plot(vertices[:,0], vertices[:,1], color=color)
# The conversion and display of the plot
app = QtGui.QApplication(sys.argv)
gcf().canvas.draw() # IMPORTANT!
stringBuffer = gcf().canvas.buffer_rgba() # IMPORTANT!
l, b, w, h = gcf().bbox.bounds
qImage = QtGui.QImage(stringBuffer,
w,
h,
QtGui.QImage.Format_ARGB32)
scene = QtGui.QGraphicsScene()
view = QtGui.QGraphicsView(scene)
pixmap = QtGui.QPixmap.fromImage(qImage)
pixmapItem = QtGui.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmapItem)
view.show()
app.exec_()
编辑 3:对于您的反转颜色图问题,请参阅 here (documentation)和 here (SO) .
PS:我建议您自己做,而不是使用诸如 qimage2ndarray
之类的额外库来将 OpenCV 图像转换为 QImage
,以查看究竟需要什么完毕。如果您对 Material 有丰富的经验并且您只需要节省一些时间,那么这样的库就很整洁。除此之外 - 远离他们。
关于python - 如何使用 pyqt 在场景中输出颜色图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35445584/
我现在已经用 PyQt 做了几个项目,而且我越来越熟悉 Qt 采用的模型/ View 思想流派。我已经将它用于列表和表格 View 之类的东西,它们背后有一个自定义模型来显示和操作数据。我使用委托(d
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MainWindow(QMainWindow):
使用下面的示例代码(受 here 的严重影响),右键单击上下文菜单并没有真正正确对齐。 从屏幕截图中可以看出,生成的菜单在鼠标光标上方相当多的位置。我希望菜单的左上角与鼠标指针完全对齐。 有没有办法对
所以我创建了一个自定义上下文菜单,但我想根据某些值将树小部件的某些行中的某些项目灰显。如何禁用菜单上的项目? myUI.setContextMenuPolicy( Qt.CustomContextMe
是否可以禁用 QComboBox在 pyqt 中,就像我们可以在 Win Forms(C#) 中那样做,因为我在 QComboBox 中找不到任何选项手动的。我想启用 QcomboBox仅当管理员登录
我想将 QComboBox 与元组中的“键”和“值”一起使用,该元组类似于 django 模型中使用的元组。例如,我对一个人的性别有以下结构。 SEX_CHOICES = (('M', 'Male')
是否可以让 Altair 或 Vega(-Lite) 渲染到 PyQt 小部件,类似于支持多个后端的 Matplotlib?我知道我可以使用 Qt WebView 小部件来呈现带有 Vega 嵌入的网
在下面的示例代码中(受 here 的影响很大),我希望选择单击单元格的整行而不是单个单元格。如何更改代码以合并它? import re import operator import os import
我正在尝试禁用关闭“x”按钮,并且我认为通过将 DockWidgetFeature 设置为仅可移动和可 float 即可工作。 def CreateDockWidget (self): Pan
我已经按照 Yasin Uludag 的一些有用的在线教程来尝试使用 PyQt(或者更确切地说是 PySide)来创建一个简单的 TreeView ,但是我在使用工具提示时遇到了问题。在以下代码中,工
我正在尝试创建一个场景,我需要从 mousePressEvent 位置画线到最新的鼠标 moveposition 这意味着我需要调用 paintEvent 来自 mousePressEvent ,这可
是Python 3的组合和 PyQt 4受到推崇的?有没有其他选择? 最佳答案 我不明白为什么不,有一个 version available对于正常工作的 Python 3,如果你真的需要 Qt,唯一
我正在尝试显示从二进制文件中读取的图像数据(我编写了用于从文件中检索此数据并将其存储为图像以供 QImage() 使用的代码)。我想做的是将 slider 连接到图形 View 小部件,以便当您移动
我已经准备了很多关于如何在 python 和 pyqt 中将多个信号连接到同一个事件处理程序的帖子。例如,将多个按钮或组合框连接到同一功能。 许多示例展示了如何使用 QSignalMapper 执行此
我有一个 PyQt 主窗口,当用户按下某个按钮时,我需要从中获取一串用户输入。 这是我的用户输入窗口代码: class InputDialog(QtGui.QDialog): ''' t
编辑: 以下 buildout.cfg 用于构建 Qt、PyQt 和 SIP [buildout] parts = pyqt [pyqt] recipe = zc.recipe.cmmi ur
我目前正在开发一个应用程序,该应用程序可以使用 PyQt 访问 sqlalchemy 数据库并将其内容显示到 TableView 或其他一些小部件中。现在为了简单起见,我们只说这是一个电话簿,上面有姓
使用我现在拥有的代码,我可以成功地播放文件中的 .mp3 数据。但是我需要使用 QtCore.QBuffer(不是来自文件)播放相同的数据。当我使用文档的示例时,它会出现意外类型的 QBuffer 错
安装 sip 后,我在尝试安装 PyQt-x11-gpl-4.11 时不断收到这个可爱的错误消息。 mycommandline$ python configure-ng.py --verbose Qu
我正在为一个项目使用 PyQt。但并非突然间我收到一个错误: QPixmap: It is not safe to use pixmaps outside the GUI thread in PyQt
我是一名优秀的程序员,十分优秀!