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. How to scale a image by QImageReader
Forum Updated to NodeBB v4.3 + New Features

How to scale a image by QImageReader

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 784 Views
  • 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.
  • feiyuhuahuoF Offline
    feiyuhuahuoF Offline
    feiyuhuahuo
    wrote on last edited by
    #1

    I try to use the following code to scale a large image to a smaller size, but it failed. And thus because the too large size, the QImage is NULL.

    from PySide6.QtCore import QSize, QRect
    from PySide6.QtGui import QImageReader
    
    img = QImageReader('Image-0015.bmp')
    size = img.size()
    print(img.size())
    img.setScaledSize(QSize(1024, 1024))
    print(img.canRead())
    print(img.size())
    new_img = img.read()
    print(new_img)
    

    799b082b-d5e5-4e4c-8c61-cd0b681f0354-image.png

    JonBJ 1 Reply Last reply
    0
    • feiyuhuahuoF feiyuhuahuo

      I try to use the following code to scale a large image to a smaller size, but it failed. And thus because the too large size, the QImage is NULL.

      from PySide6.QtCore import QSize, QRect
      from PySide6.QtGui import QImageReader
      
      img = QImageReader('Image-0015.bmp')
      size = img.size()
      print(img.size())
      img.setScaledSize(QSize(1024, 1024))
      print(img.canRead())
      print(img.size())
      new_img = img.read()
      print(new_img)
      

      799b082b-d5e5-4e4c-8c61-cd0b681f0354-image.png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @feiyuhuahuo said in How to scale a image by QImageReader:

      but it failed. And thus because the too large size, the QImage is NULL.

      I am not sure that is really the case?

      img.setScaledSize(QSize(1024, 1024))
      print(img.size())
      

      Try print(img.scaledSize())?

      new_img = img.read()
      print(new_img)
      

      If this returns a null image, try print(img.error(), img.errorString()).

      I do not know what this is supposed to do

      For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.

      feiyuhuahuoF 1 Reply Last reply
      1
      • JonBJ JonB

        @feiyuhuahuo said in How to scale a image by QImageReader:

        but it failed. And thus because the too large size, the QImage is NULL.

        I am not sure that is really the case?

        img.setScaledSize(QSize(1024, 1024))
        print(img.size())
        

        Try print(img.scaledSize())?

        new_img = img.read()
        print(new_img)
        

        If this returns a null image, try print(img.error(), img.errorString()).

        I do not know what this is supposed to do

        For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.

        feiyuhuahuoF Offline
        feiyuhuahuoF Offline
        feiyuhuahuo
        wrote on last edited by
        #3

        @JonB I want to read a large BMP image (8200 * 6000, 47MB). I tried QPixmap and QImage, but all of them failed.
        Like:

        img = QImage('Image-0015.bmp')
        print(img)
        

        169b9155-958d-4858-b076-da52d3c91f88-image.png
        I searched online and seems it's because that the image is to large. So I tried QImageReader, did I do this right?
        I don't know how to convert it to a QImage object and how to scale it to a preferred size.

        JonBJ 1 Reply Last reply
        0
        • feiyuhuahuoF feiyuhuahuo

          @JonB I want to read a large BMP image (8200 * 6000, 47MB). I tried QPixmap and QImage, but all of them failed.
          Like:

          img = QImage('Image-0015.bmp')
          print(img)
          

          169b9155-958d-4858-b076-da52d3c91f88-image.png
          I searched online and seems it's because that the image is to large. So I tried QImageReader, did I do this right?
          I don't know how to convert it to a QImage object and how to scale it to a preferred size.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @feiyuhuahuo
          I now understand what you are showing. For a .bmp which is 8200x6000 you can open it via QImageReader but not via QImage. This is not my area, but

          I searched online and seems it's because that the image is to large.

          Where did you read this? In principle I think QImage is limited to 32767x32767, maybe larger, so your size would be "surprising" to be too large. Let's start with: What platform are you on? Are you compiling for 32- or 64-bit? What is the image's file size (ah, 47MB, sounds tiny!!)? How much free memory do you have? What version of Qt are you using?

          I also told you with your earlier code

          If this returns a null image, try print(img.error(), img.errorString()).

          Can you please actually do this, why guess what the issue might be when we can ask Qt?

          feiyuhuahuoF 1 Reply Last reply
          0
          • JonBJ JonB

            @feiyuhuahuo
            I now understand what you are showing. For a .bmp which is 8200x6000 you can open it via QImageReader but not via QImage. This is not my area, but

            I searched online and seems it's because that the image is to large.

            Where did you read this? In principle I think QImage is limited to 32767x32767, maybe larger, so your size would be "surprising" to be too large. Let's start with: What platform are you on? Are you compiling for 32- or 64-bit? What is the image's file size (ah, 47MB, sounds tiny!!)? How much free memory do you have? What version of Qt are you using?

            I also told you with your earlier code

            If this returns a null image, try print(img.error(), img.errorString()).

            Can you please actually do this, why guess what the issue might be when we can ask Qt?

            feiyuhuahuoF Offline
            feiyuhuahuoF Offline
            feiyuhuahuo
            wrote on last edited by feiyuhuahuo
            #5

            @JonB I'm on a 64-bit WIN10. I have more than 20GB free memory. Qt version is 6.3.2. (PySide6 6.3.2, actually).
            Here I did a test:

            from PySide6.QtGui import QImageReader, QImage
            from PIL import Image
            
            img = QImageReader('1000000139#OK#20220928101924#84.8.bmp')
            new_i = img.read()
            print(new_i, '\n')
            print(img.error(), '\n')
            print(img.errorString(), '\n')
            print('---------------------------------------')
            img = QImageReader('Image-0015.bmp')
            new_i = img.read()
            print(new_i, '\n')
            print(img.error(), '\n')
            print(img.errorString(), '\n')
            
            img1 = Image.open('1000000139#OK#20220928101924#84.8.bmp')
            print(img1.format)
            
            img2 = Image.open('Image-0015.bmp')
            print(img2.format)
            

            9a86fbc2-eec3-4312-a5aa-a0a2558fba34-image.png
            As you can see, a 1024×1024 BMP image can be read. But the 8200×6000 BMP image can not be read. I used a image library PIL to make sure they are actually BMP images. And they certainly are.
            Besides,I read on some Chinese blogs. Someone else also encountered the same problem. They said it's because the image is too large. But I'm not sure about this.

            D 1 Reply Last reply
            0
            • feiyuhuahuoF feiyuhuahuo

              @JonB I'm on a 64-bit WIN10. I have more than 20GB free memory. Qt version is 6.3.2. (PySide6 6.3.2, actually).
              Here I did a test:

              from PySide6.QtGui import QImageReader, QImage
              from PIL import Image
              
              img = QImageReader('1000000139#OK#20220928101924#84.8.bmp')
              new_i = img.read()
              print(new_i, '\n')
              print(img.error(), '\n')
              print(img.errorString(), '\n')
              print('---------------------------------------')
              img = QImageReader('Image-0015.bmp')
              new_i = img.read()
              print(new_i, '\n')
              print(img.error(), '\n')
              print(img.errorString(), '\n')
              
              img1 = Image.open('1000000139#OK#20220928101924#84.8.bmp')
              print(img1.format)
              
              img2 = Image.open('Image-0015.bmp')
              print(img2.format)
              

              9a86fbc2-eec3-4312-a5aa-a0a2558fba34-image.png
              As you can see, a 1024×1024 BMP image can be read. But the 8200×6000 BMP image can not be read. I used a image library PIL to make sure they are actually BMP images. And they certainly are.
              Besides,I read on some Chinese blogs. Someone else also encountered the same problem. They said it's because the image is too large. But I'm not sure about this.

              D Offline
              D Offline
              DerReisende
              wrote on last edited by
              #6

              @feiyuhuahuo Have you tried increasing QImageReader::setAllocationLimit? The default limit seems to be 128MBytes (for an 8K image at 32bit according to the source).

              feiyuhuahuoF 1 Reply Last reply
              2
              • D DerReisende

                @feiyuhuahuo Have you tried increasing QImageReader::setAllocationLimit? The default limit seems to be 128MBytes (for an 8K image at 32bit according to the source).

                feiyuhuahuoF Offline
                feiyuhuahuoF Offline
                feiyuhuahuo
                wrote on last edited by
                #7

                @DerReisende said in How to scale a image by QImageReader:

                setAllocationLimit

                Thanks, it works.

                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