Python: How to Resize Raster Image with PyQt

后端 未结 1 916
忘了有多久
忘了有多久 2020-12-31 04:19

I need to find a way to re-size an input raster image (such as jpg) to a specified width/height resolution (given in pixels). It would be great if PyQt while resizing a new

相关标签:
1条回答
  • 2020-12-31 05:15

    Create a pixmap:

        pixmap = QtGui.QPixmap(path)
    

    and then use QPixmap.scaledToWidth or QPixmap.scaledToHeight:

        pixmap2 = pixmap.scaledToWidth(64)
        pixmap3 = pixmap.scaledToHeight(64)
    

    With a 2048x1024 image, the first method would result in an image that is 64x32, whilst the second would be 128x64. Obviously it is impossible to resize a 2048x1024 image to 64x64 whilst keeping the same aspect ratio (because the ratios are different).

    To avoid choosing between width or height, you can use QPixmap.scaled:

        pixmap4 = pixmap.scaled(64, 64, QtCore.Qt.KeepAspectRatio)
    

    which will automatically adjust to the largest size possible.

    To resize the image to an exact size, do:

        pixmap5 = pixmap.scaled(64, 64)
    

    Of course, in this case, the resulting image won't keep the same aspect ratio, unless the original image was also 1:1.

    0 讨论(0)
提交回复
热议问题