python-3.x - 如何在pyqt中显示RGB图像

标签 python-3.x pyqt5 python-imaging-library qdockwidget

我在 RBG 模式下有一张图像,当我尝试在 qdockWidget 上显示它时,图像显示不正确。我可以在“L”模式下打印图像。我错过了什么。提前致谢:) enter image description here

代码如下:

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import (QApplication, QMainWindow)
from PyQt5.QtCore import Qt

from reportlab.graphics.shapes import *

from PIL import ImageQt
from PIL import Image, ImageDraw


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)

        dockWidget = QtWidgets.QDockWidget()
        dockWidget.setWindowTitle("Image Viewer")

        w, h = 220, 190
        shape = [(40, 40), (w - 10, h - 10)]

        # creating new Image object
        img = Image.new("RGB", (w, h))

        # create rectangleimage
        img1 = ImageDraw.Draw(img)
        img1.rectangle(shape, fill="#800080", outline="green")
        img.show()

        QtImage1 = ImageQt.ImageQt(img)
        QtImage2 = QtGui.QImage(QtImage1)
        pixmap = QtGui.QPixmap.fromImage(QtImage2)
        label = QtWidgets.QLabel('testing', self)
        label.setPixmap(pixmap)

        dockWidget.setWidget(label)
        dockWidget.setFloating(False)
        self.addDockWidget(Qt.RightDockWidgetArea, dockWidget)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWidget = MainWindow()
    myWidget.show()

    sys.exit(app.exec_())

最佳答案

问题是“img”和“QtImage1”、“QtImage2”和“pixmap”共享同一个缓冲区导致错误,解决方法是复制图像:

qimage = ImageQt.ImageQt(img)<b>.copy()</b>
pixmap = QtGui.QPixmap.fromImage(qimage)
label = QtWidgets.QLabel()
label.setPixmap(pixmap)

关于python-3.x - 如何在pyqt中显示RGB图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62285017/

相关文章:

python - 值错误 : Too many dimensions: 3 > 2

python - PIL 错误 : The _imaging C module is not installed

Python 3 Tkinter - 更改具有相同名称的多个条目的状态

python - 根据时间戳将 txt 文件数据分割成 24 小时的 block

python - 在 QCompleter 激活调用时查找 QStandardItemModel 的索引

javascript - 如何从应用程序向 PyQtWebkitView javascript 发出信号?

python - 使用 Qlistview 设置 QStandarItemModel 中项目的选择

python - numpy.equal 与嵌套列表

python - 重新排列列表中的元素

python - pandas.read_json() 未按预期工作