我一直在尝试通过将图像添加到标签setPixmap()
但无济于事......事实上,我希望如果我点击调用 Methodedef click_photo(self):
来设置图像
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import os, sys, time
class MainWindow(QMainWindow):
def __init__(self): # constructor
super().__init__()
self.setGeometry(100, 100, 800, 600)
self.setStyleSheet("background : lightgrey;")
self.available_cameras = QCameraInfo.availableCameras()
self.Label_preview = QLabel(self, 'preview here')
self.status = QStatusBar()
self.status.setStyleSheet("background : white;")
self.setStatusBar(self.status) # adding status bar to the main window
self.save_path = "" # path to save
self.viewfinder = QCameraViewfinder() # creating a QCameraViewfinder object
self.viewfinder.show() # showing this viewfinder
self.setCentralWidget(self.viewfinder) # making it central widget of main window
self.select_camera(0) # Set the default camera.
toolbar = QToolBar("Camera Tool Bar") # creating a tool bar
self.addToolBar(toolbar) # adding tool bar to main window
click_action = QAction("Click photo", self) # creating a photo action to take photo
click_action.setStatusTip("This will capture picture") # adding status tip to the photo action
click_action.setToolTip("Capture picture")
click_action.triggered.connect(self.click_photo) # adding action to it
toolbar.addAction(click_action) # adding this to the tool bar
change_folder_action = QAction("Change save location", self) # similarly creating action for changing save folder
change_folder_action.setStatusTip("Change folder where picture will be saved saved.")
change_folder_action.setToolTip("Change save location") # adding tool tip to it
# setting calling method to the change folder action
# when triggered signal is emitted
change_folder_action.triggered.connect(self.change_folder)
toolbar.addAction(change_folder_action) # adding this to the tool bar
# creating a combo box for selecting camera
camera_selector = QComboBox()
# adding status tip to it
camera_selector.setStatusTip("Choose camera to take pictures")
# adding tool tip to it
camera_selector.setToolTip("Select Camera")
camera_selector.setToolTipDuration(2500)
# adding items to the combo box
camera_selector.addItems([camera.description()
for camera in self.available_cameras])
# adding action to the combo box
# calling the select camera method
camera_selector.currentIndexChanged.connect(self.select_camera)
# adding this to tool bar
toolbar.addWidget(camera_selector)
toolbar.setStyleSheet("background : white;")
self.setWindowTitle("PyQt5 Cam")
self.show()
# method to select camera
def select_camera(self, i):
self.camera = QCamera(self.available_cameras[i]) # getting the selected camera
self.camera.setViewfinder(self.viewfinder) # getting the selected camera
self.camera.setCaptureMode(QCamera.CaptureStillImage) # setting capture mode to the camera
self.camera.error.connect(lambda: self.alert(self.camera.errorString())) # if any error occur show the alert
self.camera.start() # start the camera
self.capture = QCameraImageCapture(self.camera) # creating a QCameraImageCapture object
self.capture.error.connect(lambda error_msg, error, msg: self.alert(msg)) # showing alert if error occur
self.capture.imageCaptured.connect(lambda d,
i: self.status.showMessage("Image captured : "
+ str(self.save_seq))) # when image captured showing message
self.capture.imageCaptured.connect(lambda d, i: self.status.showMessage("Image captured : " + str(self.save_seq))) # when image captured showing message
self.tipamu = i
# getting current camera name
self.current_camera_name = self.available_cameras[i].description()
# inital save sequence
self.save_seq = 0
# method to take photo
def click_photo(self):
# time stamp
timestamp = time.strftime("%d-%b-%Y-%H_%M_%S")
self.capture.capture(os.path.join(self.save_path,
"%s-%04d-%s.jpg" % (
self.current_camera_name,
self.save_seq,
timestamp
))) # capture the image and save it on the save path
# increment the sequence
self.save_seq += 1
# change folder method
def change_folder(self):
path = QFileDialog.getExistingDirectory(self, "Picture Location", "") # open the dialog to select path
if path: # if path is selected
self.save_path = path # update the path
self.save_seq = 0 # update the sequence
def alert(self, msg):
error = QErrorMessage(self) # error message
error.showMessage(msg) # setting text to the error message
# Driver code
if __name__ == "__main__" :
App = QApplication(sys.argv) # create pyqt5 app
window = MainWindow() # create the instance of our Window
sys.exit(App.exec()) # start the app
我试过了
self.Label_preview.setPixmap(QPixmap(self.capture))
但它没有用,有没有办法解决这个问题:将通过网络摄像头捕获的图像设置为预览 Qlabel self.Label_preview
,然后再将该图像保存到磁盘或不保存到磁盘?