如何使用 PYQt' QImage scanline() 访问像素数据



我需要使用 PyQt4 访问 qimage 对象中的像素数据。

.pixel() 太慢了,所以文档说使用 scanline() 方法。

在 c++ 中,我可以获取 scanline() 方法返回的指针,并从缓冲区读取/写入像素 RGB 值。

使用

Python 我获得了指向像素缓冲区的 SIP voidptr 对象,因此我只能使用字节数组读取像素 RGB 值,但我无法更改原始指针中的值。

有什么建议吗?

以下是一些示例:

from PyQt4 import QtGui, QtCore
img = QtGui.QImage(100, 100, QtGui.QImage.Format_ARGB32)
img.fill(0xdeadbeef)
ptr = img.bits()
ptr.setsize(img.byteCount())
## copy the data out as a string
strData = ptr.asstring()
## get a read-only buffer to access the data
buf = buffer(ptr, 0, img.byteCount())
## view the data as a read-only numpy array
import numpy as np
arr = np.frombuffer(buf, dtype=np.ubyte).reshape(img.height(), img.width(), 4)
## view the data as a writable numpy array
arr = np.asarray(ptr).reshape(img.height(), img.width(), 4)

最新更新