PSEUDO
PSEUDO

Reputation: 113

Display image in PyQt5 from numpy array

I have an numpy array with shape (500, 500, 3). And I want it to convert into image and show it using PyQt5.

Upvotes: 2

Views: 2330

Answers (1)

Heike
Heike

Reputation: 24420

I'm assuming that the array is of type uint8 and represents the red, green, and blue You could use Pillow for this, e.g.

from PyQt5 import QtWidgets, QtGui

from PIL import Image, ImageQt
import numpy as np

# generate data
table = np.zeros((256,256,3), dtype=np.uint8)
for i in range(256):
    table[:,i,0] = i
    table[i,:,1] = i
table[:,:,2] = (2*255 - table[:,:,0] - table[:,:,1]) // 2

# convert data to QImage using PIL
img = Image.fromarray(table, mode='RGB')
qt_img = ImageQt.ImageQt(img)

app = QtWidgets.QApplication([])
w = QtWidgets.QLabel()
w.setPixmap(QtGui.QPixmap.fromImage(qt_img))
w.show()
app.exec()

Upvotes: 2

Related Questions