0

I am creating a label with a pixmap in a cell of a QTableWidget, and I want to be able to "zoom" in and out of the table. I accomplish this by scaling the pixmap with .scaled(width, height) which works totally fine, however once the pixmap is scaled down I can not scale it back up again and maintain the original resolution. In other words once it has drawn to a smaller size I no longer have those pixels for later use, so when I scale it up I'm re-sampling the image.

How can I make it so that I am able to "zoom" in and then back out again of a pixmap while efficiently maintaining the image resolution? Is scaled() the wrong way to go altogether? I could theoretically just reference the original file each time I zoom in and out and create a fresh pixmap at the desired scale, but that assumes that the files are always accessible (which they may not be in my case).

EDIT: Okay so based on comments I need to create a new pixmap copy for each scale cycle, now the question is how cleanest to do that and how do I make a new instance of the pixmap without editing the original? Here's a snippet of basically what I have now:

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))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Spencer
  • 1,931
  • 1
  • 21
  • 44
  • Unless you have too many of them so that you run into memory problems, I'd just keep copies of the full-size pixmaps. A scaled-down version would go into your table widget, and when you want to zoom in, you'd show (but don't transform) the full-size copy. – Schmuddi Mar 26 '17 at 08:30
  • Thanks Schmuddi, but I'm quite new to PyQt and Python so I need a little more explanation... I would have a few hundred photos at 1920x1080, not sure what the memory requirements of that are. What I'm getting is that I need to keep an array of all pixmaps at their original resolution, and then create a scaled version of this for each cell as I resize the table? I tried it out but I'm getting the same problem, how would you create a new "version"? – Spencer Mar 26 '17 at 08:51
  • Scaling is a lossy transformation - it does not round-trip cleanly. You have no choice other than to keep copies of the full-size images. – ekhumoro Mar 26 '17 at 16:13
  • I see. I added some sample code above, now I'm trying to figure out how to make a copy of the pixmap when I scale it so that I'm not editing the "master". Probably a simple solution but I'm drawing a blank at the moment... – Spencer Mar 26 '17 at 18:18

0 Answers0