#!/usr/bin/env python3
# выводит цвета выбранного пикселя изображения
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.img = QtGui.QImage('pic.png')
self.lab = QtGui.QLabel(self)
self.xEdit = QtGui.QLineEdit(self)
self.xEdit.setText('0')
self.yEdit = QtGui.QLineEdit(self)
self.yEdit.setText('0')
pxlButton = QtGui.QPushButton('Get', self)
pxlButton.resize(pxlButton.sizeHint())
pxlButton.clicked.connect(self.getPixel)
grid = QtGui.QGridLayout()
grid.addWidget(self.xEdit, 0, 0)
grid.addWidget(self.yEdit, 0, 1)
grid.addWidget(pxlButton, 1, 0)
grid.addWidget(self.lab, 2, 0, 2, 2)
self.setLayout(grid)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Image Pixel')
self.show()
def getPixel(self):
x = int(self.xEdit.text() or 0)
y = int(self.yEdit.text() or 0)
if not (0 <= x <= self.img.width() and
0 <= y <= self.img.height()):
self.lab.setText('Error')
return
rgba = self.img.pixel(x, y)
color = QtGui.QColor(rgba)
text = 'Red: {} Green: {} Blue: {}'.format(
color.red(), color.green(), color.blue())
self.lab.setText(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()