Foreword
In the last blog
Pyqt5 and opencv change the background color of the photo – Programmer Sought
Used to draw the board, but not set the brush. So on this basis, operate on the brush. draw better
Text
First look at the operation after running
Operation
first open a picture
For example, this
After getting it, jump out of the main page in full screen
easier
There are two buttons, one is the pen, used to set the pen
the other is to save
pen
one click pen
A dialog box will pop up on the main page, as shown above
At this point, you can choose the pen, the color of the pen, the line type of the pen, etc.
After selection, you need to click the OK button. If you click Cancel, it means that the dialog box is closed, and the others do not respond.
Can’t draw on the graph
Click OK to get — pen
Click the button pen again, and you will get what you selected last time, which is equivalent to caching
As long as you don’t close the main page, there will be a cache.
Source code
main source code
import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from draft.demo_pen import Ui_Form import os class Board(QWidget): def __init__(self, picture, spin, color=Qt. black): super().__init__() self.pen = None self.paint = QPainter(self) self.spin = spin self.color=color self.picture=picture def paintEvent(self, e): if self. picture: self. paint. begin(self) self.paint.drawPixmap(0,0,self.picture) self. paint. end() def get_picture(self, picture): self. clear() self.picture=picture def get_pen(self, pen): self.pen=pen def clear(self): self. picture. fill(Qt. white) def mouseMoveEvent(self, e): if self. picture: if self. pen: self. paint. begin(self. picture) self. paint. setPen(self. pen) self. paint. drawPoint(e. pos()) self. paint. end() self. update() def change_spin(self, spin): self.spin = spin def get_color(self, color): self.color=color def save(self): image = self. picture. toImage() return image class Pen(QDialog,Ui_Form): def __init__(self, pen_style=1, sp_value=0, qss=None): super().__init__() self.qss = qss self.sb_value = sp_value self. pen_style = pen_style self. setupUi(self) self.__pen=QPen() self.initui() def initui(self): self.comboBox.addItems(['Nopen','SolidLine','DashLine','DotLine','DashDotLine','DashDotDotLine','CustomDashLine']) self.comboBox.currentIndexChanged.connect(self.change_index) self. setpen() def change_index(self): self.pen_style=self.comboBox.currentIndex() def setpen(self): self.comboBox.setCurrentIndex(self.pen_style) self.spinBox.setValue(self.sb_value) self.btncolor.setStyleSheet(self.qss) def getpen(self): index=self.comboBox.currentIndex() self.__pen.setStyle(Qt.PenStyle(index)) self.__pen.setWidth(self.spinBox.value()) color=self.btncolor.palette().color(QPalette.Button) self.__pen.setColor(color) return self.__pen def get_init_data(self): return [self. pen_style, self. sb_value, self. qss] @pyqtSlot(int) def on_spinBox_valueChanged(self, value): self.sb_value=value @pyqtSlot() def on_btncolor_clicked(self): color=QColorDialog.getColor() if color.isValid(): qss = f"background-color:rgb({color.red()},{color.green()},{color.blue()});" self.qss=qss self.btncolor.setStyleSheet(qss) @pyqtSlot() def on_loser_clicked(self): self. close() @pyqtSlot() def on_ok_clicked(self): self. accept() class Main(QWidget): def __init__(self, picture): super().__init__() self.pen_init = None self.v2 = None self.pen = None self.nums = None self.iters = None self.picture = None self.cv_img = None self.color = None self.color_change = None self. board = None self. size = None self. spin = None self.pen_window=None self.paint = None self.img = None self.filename = picture self.initui() def initui(self): self.setWindowTitle('core') self.pen_init=[1,0,None] self. set_layout() def set_layout(self): h=QHBoxLayout(self) v1= QHBoxLayout(self) self.v2=QVBoxLayout(self) v1. addWidget(self. get_paint()) widgets = self. get_widget() for i in widgets: self.v2.addWidget(i) h. addLayout(v1) h. addLayout(self. v2) return h def get_widget(self): pen=QPushButton('pen',self) pen.clicked.connect(self.pen_click) save=QPushButton('save',self) save.clicked.connect(self.save_picture) return pen, save def pen_click(self): self.pen=Pen(pen_style=self.pen_init[0],sp_value=self.pen_init[1],qss=self.pen_init[2]) self.v2.addWidget(self.pen) ret=self.pen.exec() if ret==QDialog. Accepted: self.pen_init = self.pen.get_init_data() self.board.get_pen(self.pen.getpen()) def save_picture(self): path,ok = QFileDialog.getSaveFileName(self, 'save image', '.\', '(*.jpg *.png)') if ok: img = self. board. save() img. save(path) def get_paint(self): image=QPixmap(self. filename) self. board = Board(image, self. size) return self.board class Start(QWidget): def __init__(self): super().__init__() self.filename=None self.main=None self.initui() def initui(self): self. resize(640,480) self.setWindowTitle('main page') self. set_layout() def set_layout(self): v1=QHBoxLayout(self) open_btn = self. get_btn() v1. addWidget(open_btn) return v1 def get_btn(self): btn1=QPushButton('open picture',self) btn1.clicked.connect(self.open) return btn1 def open(self): filename, ok = QFileDialog.getOpenFileName(self, 'open file', os.getcwd(), '(*.jpg *.png)') if ok: self.filename=filename self.main=Main(self.filename) self.main.setWindowModality(Qt.ApplicationModal) self. main. showMaximized() def paintEvent(self, a0): paint = QPainter(self) pixmap = QPixmap('C:/Users/520/PycharmProjects/pythonProject4/picture/picture/29.jpg') paint. drawPixmap(self. rect(), pixmap) if __name__ == '__main__': app = QApplication(sys. argv) a = Start() a. show() app.exec_()
Secondary source code
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'pen.ui' # # Created by: PyQt5 UI code generator 5.15.8 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form. setObjectName("Form") Form. resize(400, 300) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(60, 30, 111, 31)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Form) self.label_2.setGeometry(QtCore.QRect(30, 90, 61, 20)) self.label_2.setObjectName("label_2") self.comboBox = QtWidgets.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(80, 90, 141, 22)) self.comboBox.setCurrentText("") self.comboBox.setObjectName("comboBox") self.label_3 = QtWidgets.QLabel(Form) self.label_3.setGeometry(QtCore.QRect(20, 150, 72, 15)) self.label_3.setObjectName("label_3") self. spinBox = QtWidgets. QSpinBox(Form) self.spinBox.setGeometry(QtCore.QRect(81, 150, 81, 22)) self.spinBox.setObjectName("spinBox") self.label_4 = QtWidgets.QLabel(Form) self.label_4.setGeometry(QtCore.QRect(10, 210, 72, 15)) self.label_4.setObjectName("label_4") self.btncolor = QtWidgets.QPushButton(Form) self.btncolor.setGeometry(QtCore.QRect(80, 200, 93, 28)) self.btncolor.setText("") self.btncolor.setObjectName("btncolor") self.ok = QtWidgets. QPushButton(Form) self.ok.setGeometry(QtCore.QRect(260, 70, 93, 28)) self.ok.setObjectName("ok") self.loser = QtWidgets.QPushButton(Form) self. loser. setGeometry(QtCore. QRect(260, 160, 93, 28)) self.loser.setObjectName("loser") self. retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "pen set property")) self.label_2.setText(_translate("Form", "line type")) self.label_3.setText(_translate("Form", "line width")) self.label_4.setText(_translate("Form", "color")) self.ok.setText(_translate("Form", "OK")) self.loser.setText(_translate("Form", "Cancel"))
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treeHomepageOverview 259027 people are studying systematically