pyqt5——QImage与QPixmap

一、尺寸相关

1、获取QImage的尺寸

from PyQt5 import QtGui
qimage = QtGui.QImage('C:/Users/wxscn/Desktop/test.jpg')
# 直接打印rect得不到长宽
rect = qimage.rect()
# 第1种获取长宽的方法
w = rect.width()
h = rect.height()
# 第2种获取长宽的方法
w_ = qimage.width()
h_ = qimage.height()
print(rect, (w, h), (w_, h_))
# 运行结果:PyQt5.QtCore.QRect(0, 0, 536, 868) (536, 868) (536, 868)

2、获取QPixmap的尺寸

qpixmap = QtGui.QPixmap('C:/Users/wxscn/Desktop/test.jpg')
w = qpixmap.width()
h = qpixmap.height()
print((w, h))
# 运行结果:(536, 868)

3、调整QPixmap的尺寸

qpixmap = qpixmap.scaled(w, h, QtCore.Qt.KeepAspectRatio)

二、像素值相关

1、获取QImage的像素值

(r,g,b) = QtGui.QColor(qimage.pixel(x, y)).getRgb()[:-1]

三、转换相关

1、Image转化为QImage

qimage = image.toqimage()

2、cv2转化为QImage

# cv2image深度为8位
qimage = QtGui.QImage(cv2image.data, cols, rows, QtGui.QImage.Format_RGB888)
# cv2image深度为16位
qimage = QtGui.QImage(cv2image.data, cols, rows, QtGui.QImage.Format_Grayscale16)

3、QPixmap转化为QImage

qimage = QtGui.QImage(qpixmap)

4、Image转化为QPixmap

qpixmap = image.toqpixmap()

5、cv2转化为QPixmap

# cv2转化为QImage
# QImage转化为QPixmap

6、QImage转化为QPixmap

qpixmap = QtGui.QPixmap(qimage)

你可能感兴趣的:(pyqt5,python,QImage)