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. GIF smooth resize
Forum Updated to NodeBB v4.3 + New Features

GIF smooth resize

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 3.9k 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.
  • P Offline
    P Offline
    PetPython
    wrote on last edited by
    #1

    I have a problem resizing a .gif file. Qt documentation points out directly that QSize.scaled() method has Qt.KeepAspectRatio argument that should resize the .gif file smoothly preserving the original proportions:
    0_1550146691759_aspect.png

    But it doesn't work in my case. It just resizes to the exact pixels I set and ignores the third argument (the original gif's width is larger than height):
    0_1550146860927_sample.gif

    Static image has no problem with it since there's Qt.SmoothTransformation argument in QPixmap.scaled() method which works fine. For static images.

    So how can I keep the original propotions of the gif when resizing it?
    Below is my example code:

    import sys
    from   PyQt5.QtWidgets import *
    from   PyQt5.QtGui     import *
    from   PyQt5.QtCore    import *
    
    GIF = 'GIF.gif'        # Insert your gif path here
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(500, 500)
            label = QLabel()
    	
            vbox = QVBoxLayout()
            vbox.addWidget(label)
            vbox.setAlignment(Qt.AlignCenter)
            self.setLayout(vbox)
    
            gif = QMovie(GIF)
            gif.setScaledSize(QSize().scaled(300, 300, Qt.KeepAspectRatio))
            label.setMovie(gif)
            gif.start()
    
    app = QApplication(sys.argv)
    screen = Window()
    screen.show()
    sys.exit(app.exec_())
    
    1 Reply Last reply
    0
    • P Offline
      P Offline
      PetPython
      wrote on last edited by PetPython
      #2

      So I just remembered about this post and since there's still no answer, I'll post my own workaround.

      from PIL import Image
      
      def smooth_gif_resize(gif, frameWidth, frameHeight):
          gif = Image.open(gif)
          gifWidth0, gifHeight0 = gif.size
      
          widthRatio = frameWidth / gifWidth0
          heightRatio = frameHeight / gifHeight0
      
          if widthRatio >= heightRatio:
              gifWidth1 = gifWidth0 * heightRatio
              gifHeight1 = frameHeight
              return gifWidth1, gifHeight1
      
          gifWidth1 = frameWidth
          gifHeight1 = gifHeight0 * widthRatio
          return gifWidth1, gifHeight1
      

      And then your doing something like this:

      gif = QMovie(GIF)
      gifSize = QSize(*smooth_gif_resize(gif, 300, 300))
      gif.setScaledSize(gifSize)
      label.setMovie(gif)
      gif.start()
      

      You are welcome!

      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