如何使用PYQt的QImage scanline()访问像素数据 [英] How can access to pixel data with PYQt' QImage scanline()
本文介绍了如何使用PYQt的QImage scanline()访问像素数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用PyQt4访问qimage对象中的像素数据.
I need to access the pixel data in a qimage object with PyQt4.
.pixel()太慢,因此文档说要使用scanline()方法.
The .pixel() is too slow so the docs say to use the scanline() method.
在c ++中,我可以获取scanline()方法返回的指针,并从缓冲区读取/写入像素RGB值.
In c++ I can get the pointer returned by the scanline() method and read/write the pixel RGB value from the buffer.
使用Python我得到了指向像素缓冲区的SIP voidptr对象,因此我只能使用字节数组读取像素RGB值,但无法更改原始指针中的值.
With Python I get the SIP voidptr object that points to the pixels buffer so I can only read the pixel RGB value using bytearray but I cannot change the value in the original pointer.
有什么建议吗?
推荐答案
以下是一些示例:
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)
这篇关于如何使用PYQt的QImage scanline()访问像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文