【问题标题】:How to copy sections of pyQT QPixmaps without copying the whole pixmap?如何在不复制整个像素图的情况下复制 pyQT QPixmaps 的部分?
【发布时间】: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
  • 好的,我可以做一个。

标签: python qt pyqt pyqt5


【解决方案1】:

感谢 eyllanesc 的评论。 QImage 似乎表现得更好:

from PyQt5.QtGui import QPixmap, QImage
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

mosaic = QImage(file) # QImage instead of QPixmap

# the tiles are known to divide evenly
width = mosaic.width() / c
height = mosaic.height() / r

# much less memory!
minipixmaps = [QPixmap.fromImage(mosaic.copy(row*height, col*width, width, height)) for row in range(r) for col in range(c)]

【讨论】:

  • 谢谢,这很有帮助!但不应该是交换列和行,即copy(col*width, row*height, width, height)吗?
  • 抱歉@theozh 不记得了,代码库是几年前的事了,目前的工作并没有使用太多 Python,但这是很有可能的。论据看起来确实像你说的那样。
猜你喜欢
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 2017-10-01
  • 2017-10-28
  • 2012-04-06
  • 2021-12-03
  • 1970-01-01
  • 2016-01-27
相关资源
最近更新 更多