I have image that needs to be annotated ie; I should be able to draw a rectangle or a polygon or any kind of shape like using pen.
What libraries can be used in Pyqt4?
I have image that needs to be annotated ie; I should be able to draw a rectangle or a polygon or any kind of shape like using pen.
What libraries can be used in Pyqt4?
There are 2 possible solutions:
Create a class that inherits from a QLabel or QWidget and overwrite the paintEvent method.
Use QGraphicsView and add QGraphicsItem that represent those characteristics.
The second solution I think is the best so it's the one I've implemented.
import 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_())
Another example:
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_())