Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [SOLVED] Pyside, Pil image to QPixmap
Forum Update on Monday, May 27th 2025

[SOLVED] Pyside, Pil image to QPixmap

Scheduled Pinned Locked Moved Language Bindings
4 Posts 3 Posters 21.7k Views
  • 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.
  • R Offline
    R Offline
    Raschko
    wrote on last edited by
    #1

    For a few days now I've been trying to format a simple rgba pil image into a pixmap.
    So as to display it using a painter, I.E:

    @
    self.image = converted pil to pixmap

    this = QtGui.QPainter(self)
    this.drawPixmap( self.rect(), self.image )
    @

    My knowledge so far leads me to implement this conversion method:
    @
    data = self.image.tostring('raw', 'RGBA')
    image = QtGui.QImage.fromData(data)
    data = None
    pixmap = QtGui.QPixmap.fromImage(image)
    image = None
    return pixmap
    @

    However, when painting, nothing appears.
    When investigating further I noticed that the pixmap dimensions were (0,0)

    So somewhere I may be loosing some formatting?
    Or I am missing an image parameter for either a QPixmap or QImage..

    Any thoughts?

    Shop smart, shop "S"mart!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      renato.filho
      wrote on last edited by
      #2

      QImage is not able to parse the current data returned to pil image you need to inform more details to make this possible, you can use that code to create a QImage fom the pil data.

      @from PIL import Image
      from PySide.QtGui import QImage, QImageReader, QLabel, QPixmap, QApplication

      im = Image.open("my_image.png")
      data = im.tostring('raw', 'RGBA')

      app = QApplication([])

      image = QImage(data, im.size[0], im.size[1], QImage.Format_ARGB32)
      pix = QPixmap.fromImage(image)
      lbl = QLabel()
      lbl.setPixmap(pix)
      lbl.show()

      app.exec_()@

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Raschko
        wrote on last edited by
        #3

        I noticed that both our renditions work, the version of pyside I am running has a bug in QImage(str ...

        just found out

        Fedora 14 doesn't have 1.0.2, which fixed that issue.
        The current windows version seems to work fine.

        Though, I might of found a new bug in QPainter.drawImage()
        Which crashes python under windows 7

        Shop smart, shop "S"mart!

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jusic
          wrote on last edited by
          #4

          The reason why it probably crashes on Windows 7, is that the Python interpreter loses the reference to image data, and frees the memory.

          For instance, in "PIL ImageQt.py":http://www.java2s.com/Open-Source/Python/GUI/Python-Image-Library/Imaging-1.1.7/PIL/ImageQt.py.htm the image data reference is separately stored into a class member variable:

          @
          # must keep a reference, or Qt will crash!
          self.__data = data or im.tostring()
          @

          Once I did this in my conversion function/class, the conversion method works flawlessly on Windows 7. The reason why QPixmap doesn't save the data reference correctly to prevent automatic garbage collection (I guess) is a bit beyond me.

          1 Reply Last reply
          0

          • Login

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