Display Qimage with Qtgui

Display QImage with QtGui

Thanks All, I found how to do it, which is the same as Dave and Sergey:

I am using QT Creator:

In the main GUI window create using the drag drop GUI and create label (e.g. "myLabel")

In the callback of the button (clicked) do the following using the (*ui) pointer to the user interface window:

void MainWindow::on_pushButton_clicked()
{
QImage imageObject;
imageObject.load(imagePath);
ui->myLabel->setPixmap(QPixmap::fromImage(imageObject));

//OR use the other way by setting the Pixmap directly

QPixmap pixmapObject(imagePath");
ui->myLabel2->setPixmap(pixmapObject);
}

Display a QPixmap or QImage in ImageView in pyqtgraph

Use ImageView.setImage(img) the argument has to be a 3d numpy array with shape (width, height, 4). The last dimension is rgba in the range 0 to 1.

import pyqtgraph as pg
import numpy as np

if __name__ == '__main__':
app = pg.mkQApp()
w = pg.ImageView()
image = np.array([np.array([np.array([1, i*0.01, i*j*0.0001, 1]) for i in range(100)]) for j in range(100)])
w.setImage(image)
w.show()
app.exec()

Result:

result

How to display RGB image in pyqt

The problem is that "img" and "QtImage1", "QtImage2" and "pixmap" share the same buffer that causes the error, the solution is to copy the image:

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

Unable to Display Image in my PyQt program

Here's how you can convert cv2 image to pixmap

h, w, ch = cv2_image.shape

bytes_per_line = ch * w
converted = QtGui.QImage(imgComb.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)

converted = converted.scaled(target_width, target_height, Qt.KeepAspectRatio) # Only if you want to scale image

pic = QtGui.QPixmap.fromImage(converted)
#pic is pixmap which can directly be set to QLabel

Here is your updated code

from PyQt5 import QtCore, QtGui, QtWidgets
import random
import cv2

class Ui_MainWindow(object):

def mergeimages(self, img1, img2):
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2_gray, 245, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
Attack = cv2.bitwise_and(img1, img1, mask=mask)
Defense = cv2.bitwise_and(img2, img2, mask=mask_inv)
result = cv2.add(Defense, Attack)
return result

def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1000, 900)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(190, 20, 674, 478))
self.label.setStyleSheet("ImageCombined.png")
self.label.setText("")
img1 = cv2.imread("Image1.png")
img2 = cv2.imread("Image2.png")
img2 = cv2.flip(img2, 1)
imgComb = self.mergeimages(img1, img2)
# Change Start
h, w, ch = imgComb.shape

bytes_per_line = ch * w
converted = QtGui.QImage(imgComb.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)

pic = QtGui.QPixmap.fromImage(converted)

self.label.setPixmap(pic)
# Change End
self.label.setScaledContents(True)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

PyQt5 - How to display image in QMainWindow class?

QMainWindow.setCentralWidget(widget)

Sets the given widget to be the main window’s central widget.

from PyQt5.QtWidgets import QLabel, QMainWindow, QApplication, QWidget, QVBoxLayout
from PyQt5.QtGui import QPixmap
import sys

class Menu(QMainWindow):

def __init__(self):
super().__init__()
self.setWindowTitle("Title")

self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
lay = QVBoxLayout(self.central_widget)

label = QLabel(self)
pixmap = QPixmap('logo.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())

lay.addWidget(label)
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())

Sample Image

How to display image in ratio as PreserveAspectFit in Qt Widgets

A possible solution is to use QGraphicsView, QGraphicsScene and QGraphicsPixmapItem:

#include <QApplication>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>
#include <QPixmap>

class Viewer: public QGraphicsView{
public:
Viewer(QWidget *parent = nullptr): QGraphicsView(parent){
setScene(new QGraphicsScene(this));
m_pixmapItem = scene()->addPixmap(QPixmap());
setAlignment(Qt::AlignCenter);
}
QPixmap pixmap() const{
return m_pixmapItem->pixmap();
}
void setPixmap(const QPixmap &newPixmap){
m_pixmapItem->setPixmap(newPixmap);
fitInView(m_pixmapItem, Qt::KeepAspectRatio);
}
protected:
void resizeEvent(QResizeEvent *event){
QGraphicsView::resizeEvent(event);
fitInView(m_pixmapItem, Qt::KeepAspectRatio);
}
private:
QGraphicsPixmapItem *m_pixmapItem;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QPixmap pixmap(100, 200);
pixmap.fill(Qt::green);

Viewer w;
w.resize(640, 480);
w.setPixmap(pixmap);
w.show();

return a.exec();
}

Python version:

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView

class Viewer(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.setScene(QGraphicsScene(self))
self.m_pixmapItem = self.scene().addPixmap(QPixmap())
self.setAlignment(Qt.AlignCenter)

@property
def pixmap(self):
return self.m_pixmapItem.pixmap()

@pixmap.setter
def pixmap(self, newPixmap):
self.m_pixmapItem.setPixmap(newPixmap)
self.fitInView(self.m_pixmapItem, Qt.KeepAspectRatio)

def resizeEvent(self, event):
super().resizeEvent(event)
self.fitInView(self.m_pixmapItem, Qt.KeepAspectRatio)

def main():
import sys

app = QApplication(sys.argv)

pixmap = QPixmap(100, 200)
pixmap.fill(Qt.green)

w = Viewer()
w.resize(640, 480)
w.pixmap = pixmap
w.show()

sys.exit(app.exec_())

if __name__ == "__main__":
main()
``


Related Topics



Leave a reply



Submit