4

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

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

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

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

有什么建议么?

4

1 回答 1

8

这里有些例子:

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)
于 2012-07-09T17:20:25.723 回答