Search code examples
pythonpyqt5byteqpixmap

How to Set QPixmap from Bytes in PyQt5


How to set Pixmap of label from a bytes of an images file ("example.bmp"). I have searched and tried every way almost all day, but I can't find a solution.

I want to set the Pixmap of a label to display the image from the "Bytes" source, but I don't want to save the image file on disk. Is there a way to fix this problem?

Or maybe is there a way how to save the sequence of bytes into a file example2.bmp in memory (buffer or _io.BufferedReader)?

Here is my code

with open("example.bmp", 'rb') as file:
    header = file.read(53)
    pixeldata = file.read()

images = header+pixel #Bytes sequence of example.bmp file
pixmap = QPixmap.loadFromData(images)
self.label.setPixmap(pixmap)

Solution

  • Finally, i found solution from my mistakes. Thanks to @musicamante for reminding me.

    with open("example.bmp", 'rb') as file:
        header = file.read(53)
        pixeldata = file.read()
    
    images = header+pixel #Bytes sequence of example.bmp file
    
    # Create QPixmap instance
    pixmap = QPixmap()
    
    # Put bytes of example.bmp into it
    pixmap.loadFromData(images)
    
    # Apply that to the label
    self.label.setPixmap(pixmap)