Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QPixmap.scaled shows unexpected transformed image

QPixmap.scaled shows unexpected transformed image

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 537 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leon_7
    wrote on last edited by Leon_7
    #1

    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):
    Distorted.PNG .
    As a reference, I saved the numpy image before transforming it
    Undistorted.jpg
    with QImage() and QtGui.QPixmap.fromImage(qimg).scaled() using PIL:

    im = Image.fromarray(trnsfImg).convert('L')
            im.save("PathtoImage/img.jpeg")
    

    Can anyone tell me why this is happening?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #2

      @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 variable or you can use numpy.ascontiguousarray() or copy()

      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.

      1 Reply Last reply
      3
      • L Offline
        L Offline
        Leon_7
        wrote on last edited by
        #3

        numpy.ascontiguousarray() and copy() made no difference, but specifying bytesperline in QImage() seems to do the trick. I'm still wondering why this is necessary for small regions while it works fine without it for larger areas.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved