how to send opencv image to qml with python
-
wrote on 21 May 2021, 15:02 last edited by
How to convert PIL and opencv image to qml image source.
I am new at qt creator please help me.import os import re from pathlib import Path import sys from PySide2.QtGui import QGuiApplication from PySide2.QtQml import QQmlApplicationEngine from PySide2.QtCore import QObject, Slot, Signal, QUrl import sprite_slicer class MainWindow(QObject): def __init__(self): QObject.__init__(self) self.image_path = None self.sprite_slicer = sprite_slicer.SpriteSheet_Slice getPath = Signal(str) @Slot(str) def imagePath(self, filePath): self.getPath.emit(filePath) self.image_path = re.sub("file:///", "", filePath) @Slot(str, str) def checkParameter(self, row, col): if self.image_path is not None and row and col: self.sprite_slicer.loadsprites("", self.image_path, col, row) else: print("False") if __name__ == "__main__": app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() main = MainWindow() engine.rootContext().setContextProperty("backend", main) engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml")) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec_())
main.qml
import QtQuick 2.7 import QtQuick.Window 2.2 import QtQuick.Controls 2.15 import QtQuick.Dialogs 1.3 Window{ visible: true width: 640 height: 480 Button { id: btnOpen x: 60 y: 56 width: 56 height: 26 text: qsTr("Button") onPressed: { fileOpen.open() } FileDialog{ id: fileOpen title: "chose a file" folder: shortcuts.home selectMultiple: false nameFilters: ["Pictures (*.png)"] onAccepted: { backend.imagePath(fileOpen.fileUrl) } } } Button { id: button1 x: 60 y: 100 width: 56 height: 30 text: qsTr("Submit") onPressed: { backend.checkParameter(rows.text, cols.text) } } TextField { id: rows x: 157 y: 62 placeholderText: qsTr("Text Field") } TextField { id: cols x: 157 y: 114 placeholderText: qsTr("Text Field") } Image { id: image x: 78 y: 218 width: 197 height: 177 fillMode: Image.PreserveAspectFit } Image { id: image1 x: 356 y: 229 width: 214 height: 180 fillMode: Image.PreserveAspectFit } Connections{ target: backend function onGetPath(imagePath){ image.source = imagePath } } }
sprite_slicer.py
import cv2 from PIL.ImageQt import ImageQt from PIL import Image from PySide2.QtGui import QPixmap class SpriteSheet_Slice: def __init__(self): super().__init__() def loadsprites(self, filePath, rows, cols): rows = int(rows) cols = int(cols) image = cv2.imread(filePath) sprites = [] w, h, ch = image.shape width = w//cols height = h//rows for row in range(rows): for col in range(cols): l = col * width t = row * height r = (col+1)*width b = (row+1)*height crop_img = image[l:r, t:b] rgb_image = cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB) PIL_image = Image.fromarray(rgb_image).convert('RGB') sprites.append(PIL_image) print(sprites) return sprites
-
Hi and welcome to devnet,
There's no need to use Pillow here, go from OpenCV to QImage directly.
-
wrote on 22 May 2021, 07:44 last edited by
How Qimage send to qml image???
-
-
wrote on 23 May 2021, 07:33 last edited by
I dont know how. Please help me.
-
You know there's an example in the documentation I linked. I agree it's written in C++ but it's not hard to translate it to Python.
1/6