-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy2.py
More file actions
74 lines (58 loc) · 2.31 KB
/
py2.py
File metadata and controls
74 lines (58 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QFileDialog, QLineEdit, QGraphicsScene, QGraphicsView
from PyQt5.QtGui import QPixmap, QImage, QPainter, QPen, QPainterPath
from PyQt5.QtCore import Qt, QPoint
import sys
import numpy as np
import cv2
class ImageLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.path = QPainterPath()
def mousePressEvent(self, event):
self.path.moveTo(event.pos())
def mouseMoveEvent(self, event):
self.path.lineTo(event.pos())
self.update()
def mouseReleaseEvent(self, event):
self.path.lineTo(event.pos())
self.update()
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self)
painter.drawPath(self.path)
class AppDemo(QMainWindow):
def __init__(self):
super().__init__()
self.main_widget = QWidget(self)
self.setCentralWidget(self.main_widget)
self.layout = QVBoxLayout()
self.main_widget.setLayout(self.layout)
self.loadImageButton = QPushButton("Load Image")
self.loadImageButton.clicked.connect(self.loadImage)
self.layout.addWidget(self.loadImageButton)
self.imageLabel = ImageLabel()
self.layout.addWidget(self.imageLabel)
self.addLineButton = QPushButton("Add Silhouette")
self.addLineButton.clicked.connect(self.addSilhouette)
self.layout.addWidget(self.addLineButton)
self.imagePath = None
def loadImage(self):
self.imagePath, _ = QFileDialog.getOpenFileName()
if self.imagePath:
pixmap = QPixmap(self.imagePath)
self.imageLabel.setPixmap(pixmap)
def addSilhouette(self):
if self.imagePath:
# convert image to grayscale
img = cv2.imread(self.imagePath, cv2.IMREAD_GRAYSCALE)
# apply threshold to get silhouette
_, img_thresh = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV)
# save the silhouette image
cv2.imwrite('silhouette.png', img_thresh)
# show the silhouette image
pixmap = QPixmap('silhouette.png')
self.imageLabel.setPixmap(pixmap)
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())