python-opencv图像翻转

import cv2
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QPushButton


class QPixmapDemo(QWidget):
    val = 0
    signal = pyqtSignal()
    def __init__(self):
        super().__init__()
        self.setUI()
        self.setImage()
        self.signal.connect(self.setImage)
    # 设置ui界面
    def setUI(self):
        self.resize(800, 600)
        self.setWindowTitle('picture')
        self.imgLabel = QLabel()
        self.imgLabel.resize(800, 600)  # 设置label的大小,图片会适配label的大小
        self.btn = QPushButton('翻转')
        self.btn.clicked.connect(self.turn)
        self.Vbox = QVBoxLayout()
        self.Vbox.addWidget(self.imgLabel)
        self.Vbox.addWidget(self.btn)
        self.setLayout(self.Vbox)

    def setImage(self):
        src = cv2.imread('/home/ly/Desktop/picture.jpg')  # opencv读取图片
        img = self.flip(src)
        img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # opencv读取的bgr格式图片转换成rgb格式
        _image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3,
                              QtGui.QImage.Format_RGB888)  # pyqt5转换成自己能放的图片格式
        jpg_out = QtGui.QPixmap(_image).scaled(self.imgLabel.width(), self.imgLabel.height())  # 设置图片大小
        self.imgLabel.setPixmap(jpg_out)  # 设置图片显示
    def turn(self):
        self.val -= 1
        if self.val < -1:
            self.val = 1
        self.signal.emit()

    def flip(self,img):
        image = img
        flip = cv2.flip(image, self.val)
        return flip

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = QPixmapDemo()
    win.show()
    sys.exit(app.exec_())

运行结果:

发布了92 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zZzZzZ__/article/details/103355769