2

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

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

This code is working well but some size of numpy array create error called Segmentation fault. Try this line table = np.zeros((257,257,3), dtype=np.uint8)
Well I've solved this issue also by adding alpha channel to image. See here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.