PyQt Pixmap缩放,保持分辨率

2024-04-29 12:50:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个标签,在QTableWidget的单元格中有一个pixmap,我希望能够在表中“放大”和缩小。我通过使用.scaled(width, height)缩放pixmap来完成这一点,它工作得非常好,但是一旦pixmap被缩小,我就不能再次缩放它并保持原来的分辨率。换句话说,一旦它被画成更小的尺寸,我就不再有这些像素供以后使用了,所以当我放大它的时候,我正在重新采样图像。在

我怎样才能使我能够“放大”并再次缩小pixmap,同时有效地保持图像分辨率?scaled()是不是完全错了?理论上,每次放大和缩小时,我都可以引用原始文件,并以所需的比例创建一个新的pixmap,但前提是文件始终是可访问的(在我的情况下可能不是这样)。在

编辑: 好吧,根据评论,我需要为每个缩放周期创建一个新的pixmap副本,现在的问题是如何做到最干净,以及如何在不编辑原始pixmap的情况下创建pixmap的新实例?以下是我现在所掌握的基本情况:

global pixArray
pixArray = []

# pix array
label = QtGui.QLabel()
pic = QtGui.QPixmap(imageFile)
label.setPixmap(pic)
#toss this label into the master array for later
pixArray.append(label)
# apply scaled image
label = pixArray[len(pixArray)-1]
picScaled = label.pixmap()
label.setPixmap(picScaled.scaled(rowWidth, rowHeight))

# so now the user wants to scale everything, rowWidth and rowHeight have changed:
for column in range(self.table.columnCount()):
    # resize the cells
    self.table.setColumnWidth(column, rowWidth)
    self.table.setRowHeight(0, rowHeight)
    # resize the pixmap label
    item = self.table.cellWidget(0, column)
    label = pixArray[column]
    pic = label.pixmap()
    # at this point I am actually scaling the original pixmap, how to create a copy?
    item.setPixmap(pic.scaled(rowWidth, rowHeight))

Tags: the图像selftable分辨率情况columnlabel