使用python 的开源库 qimage2ndarray
qimage2ndarray文档
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from PyQt5 import QtGui
import numpy as np
import qimage2ndarray
import cv2 as cv
def save(self):
rect = self.scene().sceneRect()
self.img = QtGui.QImage(rect.width(), rect.height(), QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(self.img)
rectf = QRectF(0, 0, self.img.rect().width(), self.img.rect().height())
self.scene().render(painter, rectf, rect)
try:
npImg = qimage2ndarray.rgb_view(self.img)
filename, _ = QtWidgets.QFileDialog.getSaveFileName(
None, "save Image", self.filename, "Image Files (*.png)"
)
if filename:
cv.imwrite(filename,npImg)
cv.imshow("img",npImg)
except Exception as e:
print(e)