问题描述
根据这个答案,我做了以下抓取区域中的代码打印值:
Based on this answer https://stackoverflow.com/a/769221/544721 , I've made following code printing values in grabbed region:
import sys
from PyQt4.QtGui import QPixmap, QApplication
app = QApplication(sys.argv)
# img is QImage type
img = QPixmap.grabWindow(
QApplication.desktop().winId(),
x=00,
y=100,
height=20,
width=20,
).toImage()
for x in range(0,20):
for y in range(0,20):
print( "({},{}) = {}".format( x,y,(img.pixel(x,y)) ) )
但像素显示如下:
(0,0) = 4285163107
(0,1) = 4285163107
(0,2) = 4285163107
(0,3) = 4285163107
(0,4) = 4285163107
(0,5) = 4285163107
如何获取RGB值 QImage
(从 QPix获取)地图
)像素? (最好是在16,24,32屏幕位深度下工作的解决方案)?
How to get RGB values of QImage
(obtained from QPixmap
) pixels ? (preferably, solution working in 16,24,32 screen bit depths) ?
示例输出:
(0,0) = (0,0,0)
...
(10,15) = (127,15,256)
(Linux解决方案,用Python3编写)
(Solution for Linux, written in Python3)
推荐答案
您看到的问题是从img.pixel()返回的数字实际上是一个QRgb值,它是一个与格式无关的值。然后,您可以将其转换为正确的表示形式:
The issue you are seeing is that the number being returned from img.pixel() is actually a QRgb value that is a format independent value. You can then convert it into the proper representation as such:
import sys
from PyQt4.QtGui import QPixmap, QApplication, QColor
app = QApplication(sys.argv)
# img is QImage type
img = QPixmap.grabWindow(
QApplication.desktop().winId(),
x=00,
y=100,
height=20,
width=20,
).toImage()
for x in range(0,20):
for y in range(0,20):
c = img.pixel(x,y)
colors = QColor(c).getRgbF()
print "(%s,%s) = %s" % (x, y, colors)
输出
Output
(0,0) = (0.60784313725490191, 0.6588235294117647, 0.70980392156862748, 1.0)
(0,1) = (0.60784313725490191, 0.6588235294117647, 0.70980392156862748, 1.0)
(0,2) = (0.61176470588235299, 0.6588235294117647, 0.71372549019607845, 1.0)
(0,3) = (0.61176470588235299, 0.66274509803921566, 0.71372549019607845, 1.0)
:
这篇关于如何获取QPixmap或QImage像素的RGB值 - Qt,PyQt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!