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 782 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.
  • F Offline
    F Offline
    feiyuhuahuo
    wrote on 8 Oct 2022, 03:46 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

    J 1 Reply Last reply 8 Oct 2022, 07:15
    0
    • F feiyuhuahuo
      8 Oct 2022, 03:46

      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

      J Offline
      J Offline
      JonB
      wrote on 8 Oct 2022, 07:15 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.

      F 1 Reply Last reply 9 Oct 2022, 05:37
      1
      • J JonB
        8 Oct 2022, 07:15

        @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.

        F Offline
        F Offline
        feiyuhuahuo
        wrote on 9 Oct 2022, 05:37 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.

        J 1 Reply Last reply 9 Oct 2022, 08:14
        0
        • F feiyuhuahuo
          9 Oct 2022, 05:37

          @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.

          J Offline
          J Offline
          JonB
          wrote on 9 Oct 2022, 08:14 last edited by JonB 10 Sept 2022, 08:18
          #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?

          F 1 Reply Last reply 10 Oct 2022, 02:09
          0
          • J JonB
            9 Oct 2022, 08:14

            @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?

            F Offline
            F Offline
            feiyuhuahuo
            wrote on 10 Oct 2022, 02:09 last edited by feiyuhuahuo 10 Oct 2022, 02:10
            #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 10 Oct 2022, 06:14
            0
            • F feiyuhuahuo
              10 Oct 2022, 02:09

              @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 10 Oct 2022, 06:14 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).

              F 1 Reply Last reply 20 Oct 2022, 06:04
              2
              • D DerReisende
                10 Oct 2022, 06:14

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

                F Offline
                F Offline
                feiyuhuahuo
                wrote on 20 Oct 2022, 06:04 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