gpt4 book ai didi

python - 如何获取 QPixmap 或 QImage 像素的 RGB 值 - Qt, PyQt

转载 作者:IT王子 更新时间:2023-10-28 23:54:32 45 4
gpt4 key购买 nike

基于这个答案https://stackoverflow.com/a/769221/544721 ,我在抓取区域中制作了以下代码打印值:

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

如何获取QImage(从QPixmap获得)像素的RGB值? (最好是在 16、24、32 屏幕位深度下工作的解决方案)?

示例输出:

(0,0) = (0,0,0)
...
(10,15) = (127,15,256)

(Linux 的解决方案,用 Python3 编写)

最佳答案

您看到的问题是从 img.pixel() 返回的数字实际上是一个 QRgb 值,它是一个独立于格式的值。然后,您可以将其转换为正确的表示形式:

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)

输出

(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)

QImage docs :

The color of a pixel can be retrieved by passing its coordinates to the pixel() function. The pixel() function returns the color as a QRgb value indepedent of the image's format.

关于python - 如何获取 QPixmap 或 QImage 像素的 RGB 值 - Qt, PyQt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134597/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com