我再来张鼠绘

本文探讨了如何通过鼠标操作技巧提升绘画效果,提供了一系列实用的建议和技巧,旨在帮助艺术家和设计师更好地利用鼠标实现创意表达。
妈的纯鼠标就是画不出效果 本文转自:http://68ps.5d6d.com/thread-14049-1-23.html
以下是使用Python的OpenCV和PyQt5库实现的代码: ```python import cv2 import sys from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow from PyQt5.QtGui import QPixmap, QPainter, QPen from PyQt5.QtCore import Qt, QPoint class PaintLabel(QLabel): def __init__(self, parent=None): super().__init__(parent) self.last_point = QPoint() self.current_point = QPoint() self.drawing = False self.image = None def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.last_point = event.pos() self.drawing = True def mouseMoveEvent(self, event): if (event.buttons() & Qt.LeftButton) and self.drawing: self.current_point = event.pos() self.draw() def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: self.drawing = False def draw(self): painter = QPainter(self.pixmap()) pen = QPen(Qt.black, 2, Qt.SolidLine) painter.setPen(pen) painter.drawLine(self.last_point, self.current_point) self.last_point = self.current_point self.update() def set_image(self, image): self.image = image self.setPixmap(QPixmap.fromImage(image)) class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.label = PaintLabel() self.setCentralWidget(self.label) def set_image(self, image): self.label.set_image(image) if __name__ == '__main__': app = QApplication(sys.argv) image_path = 'test.jpg' image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) height, width, channel = image.shape window = MainWindow() window.set_window_title('Image Painter') window.setFixedSize(width, height) window.set_image(QImage(image.data, width, height, channel*width, QImage.Format_RGB888)) window.show() sys.exit(app.exec_()) ``` 该代码使用QMainWindow和QLabel控件构建一个图形用户界面,显示指定的图片,并允许使用标在图片上制线条。具体实现细节包括: - 继承QLabel并覆盖其mousePressEvent、mouseMoveEvent和mouseReleaseEvent方法,响应标事件并制线条。 - 创建一个QMainWindow实例,并在其中央设置一个PaintLabel实例。 - 调整窗口大小为图片大小,并在PaintLabel实例中显示指定的图片。 在使用时,只需指定要制的图片路径即可。注意要把图片格式转换成RGB模式,因为OpenCV默认使用BGR模式。运行后,会显示一个可以制的窗口,并在窗口标题中显示“Image Painter”字样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值