Files
ailai_show/Util/util_pic.py

46 lines
1.4 KiB
Python
Raw Normal View History

2025-07-29 13:16:30 +08:00
import logging
import cv2
from PySide6.QtGui import QPixmap, QImage
from Util.util_log import log
def cv2_to_qpixmap(cv_img):
"""将OpenCV图像转换为QPixmap"""
# img = cv_img.copy()
# cv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
try:
height, width, channel = cv_img.shape
bytes_per_line = channel * width
q_image = QImage(cv_img.data, width, height, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(q_image)
return pixmap
except Exception as e:
print(e)
log.log_message(logging.ERROR,e)
return None
# def cv2_to_qpixmap(cv_img):
# """将OpenCV图像转换为QPixmap"""
# if len(cv_img.shape) !=3:
# print("cv_img.shape !=3")
# return None
# try:
# img = cv_img.copy()
# height, width, channel = img.shape
# bytes_per_line = 3 * width
# q_img = QImage(img.data, width, height, bytes_per_line, QImage.Format_RGB888)
# return QPixmap.fromImage(q_img)
# except Exception as e:
# log.log_message(logging.ERROR, e)
# return None
# def cv2_to_qpixmap(cv_img):
# #g = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
# height, width, channel = cv_img.shape
# bytes_per_line = 3 * width
# q_img = QImage(cv_img.data, width, height, bytes_per_line, QImage.Format_RGB888)
# return QPixmap.fromImage(q_img)