QPixmap.scaled shows unexpected transformed image
-
Hello everyone,
I'm currently working on a GUI in python where I display camera and processed images. For this, I implemented a ROI-option where the user can select a certain region in the QLabel holding the image to only display this region. The image itself is a numpy array for which reason I transform it according to this code:
self.spec = fft2d.dft2(image) trnsfImg = np.log(np.abs(self.spec) + 0.00000000001) #to prevent zero division (non optimal solution) trnsfImg = trnsfImg[self.s_top:self.s_top+self.s_hei, self.s_left:self.s_left+self.s_wid] trnsfImg = (255*(trnsfImg-trnsfImg.min())/(trnsfImg.max()-trnsfImg.min())) trnsfImg = trnsfImg.astype(np.uint8) #for qlabel dtype needs to be uint8 qimg = QImage(trnsfImg, trnsfImg.shape[1], trnsfImg.shape[0], QImage.Format_Indexed8) self.spec_pix = QtGui.QPixmap.fromImage(qimg).scaled(self._ui.specLB.width(), self._ui.specLB.height()) self._ui.specLB.setPixmap(self.spec_pix)
This works fine when I'm selecting areas which are bigger than certain values. But when I select a "small" area, the image is distorted in an unexpected way (this is a snippet from the GUI):
.
As a reference, I saved the numpy image before transforming it
withQImage()
andQtGui.QPixmap.fromImage(qimg).scaled()
usingPIL
:im = Image.fromarray(trnsfImg).convert('L') im.save("PathtoImage/img.jpeg")
Can anyone tell me why this is happening?
-
@Leon_7 said in QPixmap.scaled shows unexpected transformed image:
I think this line is not making a copy of the data but just changing the stride and offset internally
trnsfImg = trnsfImg[self.s_top:self.s_top+self.s_hei, self.s_left:self.s_left+self.s_wid]
But when you use it here, the first argument is treated as just an array of bytes and includes all the data in trnsfImg which has the original dimensionality of that array (since your slicing doesn't create a copy)
qimg = QImage(trnsfImg, trnsfImg.shape[1], trnsfImg.shape[0], QImage.Format_Indexed8)
It might work to just assign the result of the array slicing to another variableor you can usenumpy.ascontiguousarray()
orcopy()
trnsfImg = numpy.ascontiguousarray(trnsfImg[self.s_top:self.s_top+self.s_hei, self.s_left:self.s_left+self.s_wid])
or
trnsfImg = trnsfImg[self.s_top:self.s_top+self.s_hei, self.s_left:self.s_left+self.s_wid].copy()
Supplying the original width of trnsfImg as the 'bytesperline' parameter to QImage() might also work.