【发布时间】:2018-03-25 19:20:40
【问题描述】:
我有一个包含图块的图像。我想为 QPixmap 中的每个图块制作一个 QPixmap。我认为 QPixmap 的 copy(x,y,width,height) 方法会为我完成此操作,但必须复制整个图像,而不仅仅是参数定义的矩形,因此会消耗太多内存。
下面的一个例子说明了这个问题。我的 png 恰好是 3400x3078 像素,除以 57 行 50 列,每个 minipixmap 应该是 68x54 像素,但显然使用的内存要多得多。 我错过了什么?
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication
import os, sys
app = QApplication(sys.argv)
file = r"/path/to/image/grid-of-tiles.png"
# image I'm using has these many rows and columns of tiles
r = 57
c = 50
pixmap = QPixmap(file)
# the tiles are known to divide evenly
width = pixmap.width() / c
height = pixmap.height() / r
# after executing the below, mem use climbs north of 9GB!
# I was expecting on the order of twice the original image's size
minipixmaps = [pixmap.copy(row*height, col*width, width, height) for row in range(r) for col in range(c)]
【问题讨论】:
-
使用 QImage 代替 QPixmap。更多信息:stackoverflow.com/questions/10307860/…
-
这就是我所缺少的,谢谢!我想如果你把你的评论作为答案,我会这样标记。
-
发布一个解释您的解决方案的答案:P
-
好的,我可以做一个。