I have a grayscale image in PyQt and want to get the color of a specific pixel. Grayscale images use a color table with up to 256 entries.
from PyQt4 import QtGui
def test():
image = QtGui.QImage(100, 100, QtGui.QImage.Format_Indexed8)
image.load("d:/1.bmp")
print image.pixel(1, 1)
print image.pixelIndex(1, 1)
image.setColorTable(list([i] for i in range(256)))
print image.colorTable()
import sys
app = QtGui.QApplication(sys.argv)
window = test()
sys.exit(app.exec_())
I have the following issues:
image.colorTable()
returns a list of 256 times the number 4294967295L (which is 2^32-1) altough I have just set the color table to 0 to 255.image.pixelIndex(1, 1)
gives the message "QImage::pixelIndex: Not applicable for 32-bpp images (no palette)" although the format is set to Indexed8 (and isGrayscale() returns true).image.pixel(1, 1)
returns 4278190080 (also when I set the format to Format_RGB32). What is this color? (It should be black.)
New code according to the answer by ekhumoro:
from PyQt4 import QtGui
def test():
image = QtGui.QImage(100, 100, QtGui.QImage.Format_Indexed8)
image.load("d:/1.bmp")
image2 = image.convertToFormat(QtGui.QImage.Format_Indexed8)
print "format:", image2.format()
print "pixel color:", QtGui.qGray(image2.pixel(1, 1))
image2.setColorTable(list([QtGui.qRgb(i, i, i)] for i in range(256)))
print "color table:", image2.colorTable()
import sys
app = QtGui.QApplication(sys.argv)
window = test()
sys.exit(app.exec_())