【发布时间】:2018-05-31 19:44:09
【问题描述】:
目前,我有一个图像(numpy),我想用颜色在 QLabel 中绘制它。
可以找到类似的演示:https://matplotlib.org/users/image_tutorial.html。 matplotlib 可以使用带有颜色图的 imshow 显示图像。
现在,我可以在 QLabel 中显示灰度图像,但我不知道如何将其显示为伪彩色。
用于显示灰色图像的代码是(self.img是我的):
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import numpy as np
import qimage2ndarray
class MyLabel(QLabel):
def __init__(self):
super(MyLabel, self).__init__()
img = np.zeros((256,256))
img[0:128,0:128] = 255
self.img = img
def paintEvent(self, QPaintEvent):
super(MyLabel, self).paintEvent(QPaintEvent)
QImg = qimage2ndarray.gray2qimage(self.img)
pos = QPoint(0, 0)
source = QRect(0, 0, 256,256)
painter = QPainter(self)
painter.drawPixmap(pos, QPixmap.fromImage(QImg), source)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QHBoxLayout(self)
self.label = MyLabel()
layout.addWidget(self.label)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
【问题讨论】: