0

** Version Information:

  1. Python: 3.7.9 - anaconda environment
  2. numpy = 1.19.4
  3. Pyqt5 **

I am working on an application wherein I load a binary (.bin) file and convert it to a 2D numpy array. I can read the binary file and I have also managed to convert it to a numpy array. The name of the variable is out and its shape is (512, 1536). As the shape of the array is 2D, it is a grayscale image. The variable contains:

out = array([[  0, 107,  68, ...,   0,   2,   3],
       [ 47,  65,  66, ...,   0,   1,   0],
       [ 27,  24,  34, ...,   2,   0,   1],
       ...,
       [124, 127, 124, ..., 136, 140, 147],
       [125, 129, 125, ..., 130, 134, 132],
       [130, 126, 126, ..., 139, 134, 130]], dtype=uint8)

I used opencv to show the image:

import cv2
cv2.imshow('out', out)

This works and I can see the grayscale image in a new window.

However, when I try to convert the numpy array to Pixmap using the following code the program crashes without outputting any error:

qImg = QPixmap(QImage(out.data, out.shape[1], out.shape[0], QImage.Format_Grayscale8))
print(qImg)

The print(qImg) line should output a pixmap object but it doesn't and just crashes.

I have also tried a few other things: From this (PyQt5 QImage from Numpy Array) question, I have tired using qimage2ndarray library to convert the numpy array to QImage. Converting to QImage is not a problem. But when QPixmap object is created the program crashes.

q = qimage2ndarray.array2qimage(out)
pix = QPixmap.fromImage(q)  # After this line the program crashes

Complete script:

import numpy as np
import sys
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel
import matplotlib.pyplot as plt
import qimage2ndarray

var = np.array(np.loadtxt('out.txt')).astype(np.uint8)
new_var = var.copy()
q = qimage2ndarray.array2qimage(new_var)
pix = QPixmap.fromImage(q)  # Program crashes here

app = QApplication(sys.argv)
w = QLabel()
w.setPixmap(pixmap)
w.show()
sys.exit(app.exec_())

Here the 'out.txt' file is a text file which contains the numpy array.

What could the reason for this be?

Appreciate your help.

Thank You.

Edit 1:

According to this link(Converting numpy image to QPixmap) I also tried the following (extension of the script):

GRAY_COLORTABLE = [qRgb(i, i, i) for i in range(256)]

    img_array = out.copy()
    bytesPerLine = img_array.strides[1]

    print(f'strides:{img_array.strides[1]}') # Output: 1


    height, width = img_array.shape

    image = QImage(img_array.data,
                   width, height,
                   bytesPerLine,
                   QImage.Format_Indexed8)
    image.setColorTable(GRAY_COLORTABLE)
    print('image: ', image)
    print('test')
    new = image.copy()
    pixmap = QPixmap.fromImage(new) # Program crashes here
    print(pixmap)

And Still I cannot print pixmap object. Cannot get my head around it.

Appreciate your help. Please let me know if something else is needed.

duddal
  • 19
  • 2
  • 7

1 Answers1

0

If anyone comes across this I made a silly mistake. In order to use QPixmap, the QApplication should be created first. The following lines needed to be swapped.

app = QApplication(sys.argv)
pix = QPixmap.fromImage(q)
duddal
  • 19
  • 2
  • 7