Results 1 to 6 of 6

Thread: PyQt code to draw image from raw data?

  1. #1
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default PyQt code to draw image from raw data?

    I'm trying to write PyQt code equivalent to the following C++ code, but I'm stuck. I'm relatively new to Python, and I'm having a hard time following the PyQt documentation for QImage.
    I can create the GUI parts, but how do I set up the raw data and call the QImage constructor?

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. // Create the Qt application and GUI components
    4. QApplication app(argc, argv);
    5. QGraphicsView view(&canvas, &win);
    6.  
    7. unsigned char* imageData = new unsigned char[324];
    8.  
    9. // Generate some image data
    10. for(int i = 0; i < 324; i++)
    11. {
    12. imageData[i] = i % 250;
    13. }
    14.  
    15. // Create the colormap for the indexed image
    16. QVector<QRgb> colormap;
    17. for(int i = 0; i < 255; i++)
    18. {
    19. colormap.append(qRgb(i, i, i));
    20. }
    21.  
    22. QImage img(imageData, 18, 18, QImage::Format_Indexed8); // Create the image
    23. img.setColorTable(colormap); // Set up the colormap
    24. QPixmap pix = QPixmap::fromImage(img); // Convert it to a pixmap so we can display it
    25. canvas.addPixmap(pix); // Add the pixmap to the graphics canvas
    26.  
    27. win.show();
    28. app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Here's the Python code I've got so far:
    Qt Code:
    1. from PyQt4.QtGui import *
    2. from os import sys
    3.  
    4. app = QApplication(sys.argv)
    5.  
    6. # Create the window and the graphics view
    7. win = QMainWindow()
    8. canvas = QGraphicsScene()
    9. view = QGraphicsView(canvas, win)
    10. view.resize(200, 200)
    11.  
    12. # Create the image data - How?
    13. # Create the QImage - How?
    14.  
    15. # Create the colormap
    16.  
    17. pixmap = QPixmap.fromImage(img)
    18.  
    19. # Add the image to the canvas
    20. canvas.addPixmap(pixmap)
    21.  
    22. win.show()
    23. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PyQt code to draw image from raw data?

    Something like this, maybe?

    Python Code:
    1. imageData = []
    2.  
    3. for i in range(0, 324):
    4. imageData[i] = i % 250
    5.  
    6. colormap = []
    7.  
    8. for i in range(0, 255):
    9. colormap.append(qRgb(i, i, i))
    10.  
    11. img = QImage(imageData, 18, 18, Format_Indexed8)
    12. img.setColorTable(colormap);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PyQt code to draw image from raw data?

    Unfortunately, it doesn't seem to be that easy.

    When I run the line img = QImage(imageData, 10, 10, QImage.Format_Indexed8), I get a bunch of errors saying that the arguments provided don't match any overloaded constructor:
    Qt Code:
    1. QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'list'
    2. QImage(str, int, int, int, QImage.Format): argument 1 has unexpected type 'list'
    3. QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'list'
    4. etc.
    To copy to clipboard, switch view to plain text mode 

    I think I need to do something with a "sip.voidptr", but it appears that's essentially just a C++ interface, and not something I can really work with in Python - is that correct?

  4. #4
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PyQt code to draw image from raw data?

    Quote Originally Posted by StevenB View Post
    I think I need to do something with a "sip.voidptr", but it appears that's essentially just a C++ interface, and not something I can really work with in Python - is that correct?
    I don't know much about bindings, maybe someone else can help.

  5. #5

    Default Re: PyQt code to draw image from raw data?

    Okay, firstly where are you getting this image data from? Is it a SWIGPy object?
    I had the same problem a couple of days where I had a python binding for my camera driver library and the only way of getting image data was an unsigned char*. Which obviously cannot be understood by python because SWIG did the binding.
    So when I access my unsigned char* image data directly in python here's what I get:
    <Swig Object of type 'unsigned char *' at 0xb77b0bd8>
    Anyways i wrote a small interface code to convert any unsigned char* image data to PIL image. which you can convert to QImage using ImageQt
    Here's the link to the code
    http://www.optionsbender.com/technol...datatopilimage
    and a similar post
    http://stackoverflow.com/questions/3...mage-to-python

  6. #6
    Join Date
    Aug 2009
    Posts
    52
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PyQt code to draw image from raw data?

    I found a solution:

    Qt Code:
    1. #!/usr/bin/env python
    2. # -*- coding: UTF-8 -*-
    3.  
    4. import sys
    5. import ctypes
    6. from PyQt4 import QtGui, QtCore
    7. import sip
    8.  
    9. '''
    10. 2009-2010 dbzhang800@gmail.com
    11. '''
    12.  
    13. class Window(QtGui.QWidget):
    14. def __init__(self, parent=None):
    15. super(Window, self).__init__(parent)
    16. self.label = QtGui.QLabel(self)
    17. self.label.setMinimumSize(512, 512)
    18. self.comboBox = QtGui.QComboBox(self)
    19. self.comboBox.addItems(["Image.{}".format(i) for i in range(2)])
    20. vbox = QtGui.QVBoxLayout(self)
    21. vbox.addWidget(self.label)
    22. vbox.addWidget(self.comboBox)
    23. self.initImages()
    24. self.comboBox.currentIndexChanged.connect(self.onCurrentIndexChanged)
    25.  
    26. def onCurrentIndexChanged(self, index):
    27. self.label.setPixmap(QtGui.QPixmap.fromImage(self.images[index]))
    28.  
    29. def initImages(self):
    30. self.images = []
    31. self.colorTable = [QtGui.qRgb(i, i, i) for i in range(256)]
    32. self.createImage0()
    33. self.createImage1()
    34. self.label.setPixmap(QtGui.QPixmap.fromImage(self.images[0]))
    35.  
    36. def createImage0(self):
    37. '''Create an QImage object, the copy data from other buffer to the image buffer.
    38. '''
    39. image = QtGui.QImage(512, 512, QtGui.QImage.Format_Indexed8)
    40. image.setColorTable(self.colorTable)
    41. buff = ctypes.create_string_buffer('\xFF'*512*16, 512*16)
    42. buff2 = ctypes.create_string_buffer('\x1f'*512*32, 512*32)
    43. img_ptr = image.bits()
    44. ctypes.memmove(int(img_ptr), buff, buff._length_)
    45. ctypes.memmove(int(img_ptr)+buff._length_, buff2, buff2._length_)
    46. ctypes.memmove(int(img_ptr)+buff._length_+buff2._length_, buff, buff._length_)
    47. self.images.append(image)
    48.  
    49. def createImage1(self):
    50. '''we already have a buffer, create an QImage object using the buffer.
    51. '''
    52. self.buff = ctypes.create_string_buffer('\x7F'*512*512)
    53. image = QtGui.QImage(sip.voidptr(ctypes.addressof(self.buff)), 512, 512, QtGui.QImage.Format_Indexed8)
    54. image.setColorTable(self.colorTable)
    55. self.images.append(image)
    56.  
    57. if __name__ == "__main__":
    58. app = QtGui.QApplication(sys.argv)
    59. w = Window()
    60. w.show()
    61. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Draw Image into Pixmap/Image
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 19th May 2010, 07:56
  2. How to draw a cruve with increasing data?
    By anson97209 in forum Qwt
    Replies: 17
    Last Post: 30th November 2009, 15:35
  3. Replies: 3
    Last Post: 22nd October 2009, 08:46
  4. Replies: 6
    Last Post: 21st September 2009, 11:55
  5. Draw New data, without erasing old ones
    By linuxdev in forum Newbie
    Replies: 11
    Last Post: 7th January 2009, 09:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.