1 year ago

#76802

test-img

MDDawid1

PySide6 pixelates PNGs

When im trying to add a PNG to PySide6 GUI it loses in quality. Here is the code:

import sys
import random
from PySide6 import QtCore, QtWidgets, QtGui
from PySide6.QtGui import QPalette, QColor

BOARD_SIZE = 4


class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.resize(500, 300)
        self.setWindowTitle("2048")

        self.initUI()

    def initUI(self):
        grid = QtWidgets.QGridLayout()

        # QtWidgets.QApplication.setGraphicsSystem('native')

        for i in range(BOARD_SIZE):
            for j in range(BOARD_SIZE):
                path = "./img/" + randomTile() + ".png"
                img = QtGui.QImage(path)
                pixmap = QtGui.QPixmap(img.scaledToWidth(150))
                label = QtWidgets.QLabel()
                label.setPixmap(pixmap)
                grid.addWidget(label, i, j)

        self.setLayout(grid)


def randomTile():
    tiles = ['tile0', 'tile2', 'tile4', 'tile8', 'tile16', 'tile32', 'tile64', 'tile128', 'tile256', 'tile512',
             'tile1024', 'tile2048', 'tile4096', 'tile8192', 'tile16384', 'tile32768', 'tile65536', 'tile131072']
    return tiles[random.randint(0, len(tiles) - 1)]


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.show()

    sys.exit(app.exec())

Original image:

Original image

After the quality loss:

After the quality loss

The .scaledToWidth() function isn't the problem, as the image without it in the program is still pixelated:

no .scaledToWidth()

I would like to emphasize that the problem doesn't lay in scaling. Quality is messed up even without scaling.

python

qt

pyqt

pyside6

0 Answers

Your Answer

Accepted video resources