我有需要注释的图像,即;我应该可以绘制一个矩形或多边形或任何类型的形状,如使用笔。
Pyqt4中可以使用哪些库?
答案 0 :(得分:1)
有两种可能的解决方案:
创建一个继承自QLabel或QWidget的类,并覆盖paintEvent方法。
使用QGraphicsView并添加代表这些特征的QGraphicsItem。
我认为第二种解决方案是最好的,所以它是我实施的那种。
导入sys
import math
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.btn = QPushButton("Press Me")
self.gv = QGraphicsView()
self.scene = QGraphicsScene(self)
self.gv.setScene(self.scene)
self.gv.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
lay = QVBoxLayout(self)
lay.addWidget(self.btn)
lay.addWidget(self.gv)
self.p_item = self.scene.addPixmap(QPixmap("lena.png"))
self.p_item.setPos(100, 100)
self.btn.clicked.connect(self.print_)
self.emulate_drawing()
def emulate_drawing(self):
rect = self.p_item.boundingRect()
path = QPainterPath()
A = rect.height()
for i in range(0, int(rect.width())-10):
path.lineTo(i, A/2*(1-math.cos(i*math.pi/40)))
item = QGraphicsPathItem(path, self.p_item)
item.setPen(QPen(Qt.red, 3))
r = QRectF(QPointF(), rect.size()*0.5)
item = QGraphicsEllipseItem(r, self.p_item)
item.setPos(rect.center()-r.center())
item.setPen(QPen(Qt.blue, 4))
def print_(self):
image = QImage(self.gv.viewport().size(), QImage.Format_RGB32)
image.fill(Qt.transparent)
painter = QPainter(image)
self.gv.render(painter)
painter.end()
image.save("output.png")
def showEvent(self, event):
QWidget.showEvent(self, event)
self.gv.fitInView(self.scene.sceneRect())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
另一个例子:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.btn = QPushButton("Add Line")
self.gv = QGraphicsView()
self.scene = QGraphicsScene(self)
self.gv.setScene(self.scene)
self.gv.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
lay = QHBoxLayout(self)
lay.addWidget(self.btn)
lay.addWidget(self.gv)
self.p_item = self.scene.addPixmap(QPixmap("lena.png"))
self.btn.clicked.connect(self.add_line)
def add_line(self):
p1 = self.p_item.boundingRect().topLeft()
p2 = self.p_item.boundingRect().center()
line = QGraphicsLineItem(QLineF(p1, p2), self.p_item)
line.setPen(QPen(Qt.red, 5))
line.setFlag(QGraphicsItem.ItemIsMovable, True)
line.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.gv.fitInView(self.scene.sceneRect())
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())