Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. how to send opencv image to qml with python
Forum Updated to NodeBB v4.3 + New Features

how to send opencv image to qml with python

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Toso
    wrote on 21 May 2021, 15:02 last edited by
    #1

    How to convert PIL and opencv image to qml image source.
    I am new at qt creator please help me.

    main.py

    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
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 May 2021, 19:15 last edited by
      #2

      Hi and welcome to devnet,

      There's no need to use Pillow here, go from OpenCV to QImage directly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T Offline
        T Offline
        Toso
        wrote on 22 May 2021, 07:44 last edited by
        #3

        How Qimage send to qml image???

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 May 2021, 18:49 last edited by
          #4

          QQuickImageProvider.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • T Offline
            T Offline
            Toso
            wrote on 23 May 2021, 07:33 last edited by
            #5

            I dont know how. Please help me.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 23 May 2021, 19:07 last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              21 May 2021, 15:02

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved