Although I choose the same camera frame size and widget size, white spaces appear around the window.
How can I remove these whites?
I never understood why these white parts were appearing.
Although I choose the same camera frame size and widget size, white spaces appear around the window.
How can I remove these whites?
I never understood why these white parts were appearing.
'''
class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray)
def __init__(self):
super().__init__()
self._run_flag = True
def run(self):
# capture from analog camera
cap = cv2.VideoCapture(0)
cap.set(3,720)
cap.set(4,576)
while self._run_flag:
now1=time.time()
ret, cv_img = cap.read()
if ret:
cv_img = cv2.resize(cv_img, (1024, 768))
self.change_pixmap_signal.emit(cv_img)
now2=time.time()
cap.release()
def stop(self):
#stop capture
self._run_flag = False
self.wait()
class App(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("window")
self.disply_width = 1024
self.display_height = 768
# create the label that holds the image
self.image_label.resize(self.disply_width, self.display_height)
# create a vertical box layout and add the two labels
vbox = QVBoxLayout()
vbox.addWidget(self.image_label)
#vbox.addWidget(self.textLabel)
# set the vbox layout as the widgets layout
self.setLayout(vbox)
# create the video capture thread
self.thread = VideoThread()
# connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
# start the thread
self.thread.start()
def closeEvent(self, event):
self.thread.stop()
event.accept()
@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
qt_img = self.convert_cv_qt(cv_img)
self.image_label.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(self.disply_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
if __name__ == "__main__":
app = QApplication(sys.argv)
a = App()
a.show()
#a.showFullScreen()
sys.exit(app.exec_())
'''
CodePudding user response:
Use vbox.setContentsMargins(0, 0, 0, 0);
to get rid of the white margins.
Plus there are many many more bugs in your code. For example you should not resize the image label self.image_label.resize(self.disply_width, self.display_height)
(I guess this should crash, since you have not created the label yet... but I guess you did not show all your code). You should resize the whole window instead, i.e. self.resize(self.disply_width, self.display_height)
. Well, it seems you have no understanding how layouts work. But this my answer is not intended to teach you, I am only trying to answer your question.