I would like to display an image using multiple label in a GUI(Qt Designer). The image file should be grab from current working directory and display on it own label upon user press Push Button.
Image can be displayed in label_2 when i hardcoded the image directory path but not for label_1.
def capture_image(self):
cam = cv2.VideoCapture(0)
print('Execute_captureImage')
i = 1
while i <= int(self.qty_selected):
# while i < 2:
ret, frame = cam.read()
cv2.imshow('Please review image before capture', frame)
if not ret:
break
k = cv2.waitKey(1)
if k % 256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
if k % 256 == 32:
# SPACE pressed
self.img_name = self.lotId + '_{}.jpeg'.format(i)
path = 'c:\\Users\\Desktop\\Python\\Testing' + '\\' + self.lotId + '\\' + self.img_name
print('CurrentImage = ' + path)
if not os.path.exists(path):
print('Not yet exist')
cv2.imwrite(
os.path.join('c:\\Users\\Desktop\\Python\\Testing' + '\\' + self.lotId,
self.img_name),
frame)
print("{}".format(self.img_name))
# i += 1
break
else:
print('Image already exist')
i += 1
cam.release()
cv2.destroyAllWindows()
def display_image(self):
label_vid01 = 'c:\\Users\\Desktop\\Python\\Testing' + '\\' + self.lotId + '\\' + self.img_name
label_vid02 = 'c:\\Users\\Desktop\\Python\\Testing' + '\\' + self.lotId + '\\' + self.img_name
# label_vid03 = 'c:/Users/Desktop/Python/Image/image3.jpg'
self.label_vid01.setScaledContents(True)
self.label_vid02.setScaledContents(True)
self.label_vid03.setScaledContents(True)
self.label_vid01.setPixmap(QtGui.QPixmap(label_vid01))
self.label_vid02.setPixmap(QtGui.QPixmap(label_vid02))
print(repr(label_vid01))
print(os.path.exists(label_vid01))
May i know where is the mistake?
'self.lotId' is input text based on user input.
The demo script below works for me on Windows XP. If this also works for you, the problem must be in the capture_image
function in your example (which I cannot test at the moment).
import sys, os
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QtGui.QVBoxLayout(self)
self.viewer = QtGui.QListWidget(self)
self.viewer.setViewMode(QtGui.QListView.IconMode)
self.viewer.setIconSize(QtCore.QSize(256, 256))
self.viewer.setResizeMode(QtGui.QListView.Adjust)
self.viewer.setSpacing(10)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
self.edit = QtGui.QLineEdit(self)
layout.addWidget(self.viewer)
layout.addWidget(self.edit)
layout.addWidget(self.button)
def handleButton(self):
self.viewer.clear()
name = self.edit.text()
for index in range(3):
pixmap = QtGui.QPixmap()
path = r'E:\Python\Testing\%s\%s_%d.jpg' % (name, name, index + 1)
print('load (%s) %r' % (pixmap.load(path), path))
item = QtGui.QListWidgetItem(os.path.basename(path))
item.setIcon(QtGui.QIcon(path))
self.viewer.addItem(item)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 150, 650, 500)
window.show()
sys.exit(app.exec_())