#最全面# Python 中如何将 Pyqt5 下的 QImage 对象转换成 PIL image 或 opencv MAT (numpy ndarray) 对象
1. 将QImage转化成 opencv 下的 MAT(numpy ndarray) 对象:from PyQt5.QtGui import QImagedef QImageToCvMat(incomingImage):'''Converts a QImage into an opencv MAT format'''incomingImage = incoming...
·
1. 将QImage转化成 opencv 下的 MAT(numpy ndarray) 对象:
from PyQt5.QtGui import QImage
def QImageToCvMat(incomingImage):
''' Converts a QImage into an opencv MAT format '''
incomingImage = incomingImage.convertToFormat(QImage.Format.Format_RGBA8888)
width = incomingImage.width()
height = incomingImage.height()
ptr = incomingImage.bits()
ptr.setsize(height * width * 4)
arr = np.frombuffer(ptr, np.uint8).reshape((height, width, 4))
return arr
2. 将QImage对象转化为 PIL image 对象
import io
from PIL import Image
from PyQt5.QtGui import QImage
import numpy as np
from PyQt5.QtCore import QBuffer
def QImage2PILImage(img):
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
img.save(buffer, "PNG")
pil_im = Image.open(io.BytesIO(buffer.data()))
return pil_im
更多推荐
所有评论(0)