如何在 Python 中显示当前工作目录中的图像

How to display image from current working directory in Python

我想在 GUI (Qt Designer) 中使用多个标签显示图像。图像文件应该从当前工作目录中抓取,并在用户按下按钮时显示在它自己的标签上。

当我对图像目录路径进行硬编码时,图像可以显示在 label_2 中,但不能显示在 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))

请问哪里错了?

'self.lotId' 是基于用户输入的输入文本。

label snapshot

下面的演示脚本适用于 Windows XP。如果这也适用于您,那么问题一定出在您示例中的 capture_image 函数中(我目前无法测试)。

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_())