numpy 數組轉 QImage 然後顯示到 QLabel

pyqt5不熟,百度看大佬示例,搞了半小時,記錄一下

外部鏈接:
https://blog.csdn.net/qq_32973061/article/details/81139689
https://blog.csdn.net/ccchen706/article/details/71425653

from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QImage, QPixmap
import numpy as np

if __name__ == '__main__':
    app = QApplication(sys.argv)
    h, w = 300, 600
    im = np.random.randint(0, 255, [h, w, 3], np.uint8)
    a = QLabel()
    a.resize(w, h)
    # 注意下面QtGui.QImage的第四個參數,意思爲圖像每行有多少個字節,不設定時,圖像有時會歪,所以一定要設定
    im = QImage(im.data, im.shape[1], im.shape[0], im.shape[1]*3, QImage.Format_RGB888)
    pix = QPixmap(im).scaled(a.width(), a.height())
    a.setPixmap(pix)
    a.show()
    exit(app.exec_())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章