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. PySide QImage constructor from buffer doesn't copy memory
Forum Updated to NodeBB v4.3 + New Features

PySide QImage constructor from buffer doesn't copy memory

Scheduled Pinned Locked Moved Language Bindings
3 Posts 2 Posters 2.6k 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
    titusjan
    wrote on 10 May 2013, 14:06 last edited by
    #1

    Hello all

    When I create a QImage from a buffer and then delete the buffer (or the buffer goes out of scope), the QImage object refers to an unreserved piece of memory. This leads to unexpected behavior as illustrated by the program below. The program saves two blue images whereas I would expect "savedimg1.png" to be red.

    I think this is a bug but I thought I'd ask here first before I submit a bug report. BTW, PyQt4 does save a red and blue image.

    @
    import sys

    if True:
    from PySide import QtGui
    IMG_FORMAT = QtGui.QImage.Format.Format_RGB32
    else:
    from PyQt4 import QtGui
    IMG_FORMAT = QtGui.QImage.Format_RGB32

    WIDTH = 255
    HEIGHT = 128

    def create_buf(r, g, b):
    color = chr(b) + chr(g) + chr(r) + chr(255)
    pixels = (WIDTH*HEIGHT) * color
    return buffer(pixels)

    app = QtGui.QApplication(sys.argv)

    buf1 = create_buf(255, 0, 0)
    img1 = QtGui.QImage(buf1, WIDTH, HEIGHT, IMG_FORMAT)
    print "buf1: {!r}".format(buf1)

    del buf1 # Removing this line will cause savedimg1.png to be red

    buf2 = create_buf(0, 0, 255)
    print "buf2: {!r}".format(buf2)
    img2 = QtGui.QImage(buf2, WIDTH, HEIGHT, IMG_FORMAT)

    print "img1 buffer: {!r}".format(img1.constBits())
    print "img2 buffer: {!r}".format(img2.constBits())

    img1.save('savedimg1.png')
    img2.save('savedimg2.png')
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      titusjan
      wrote on 10 May 2013, 14:10 last edited by
      #2

      Forgot to mention: I'm using Python 2.7.3 and PySide 4.8.4 on OS-X 10.6.8

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 May 2013, 21:26 last edited by
        #3

        Hi,

        AFAIK, when you use the constructor variant with the buffer, you must ensure the buffer exists through all the lifetime of the QImage. Check the QImage documentation.

        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

        3/3

        10 May 2013, 21:26

        • Login

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