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

时间:2016-10-10 08:01:34

标签: user-interface python-3.4 qt-designer qlabel

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

当我硬编码图像目录路径而不是label_1时,图像可以显示在label_2中。

  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

1 个答案:

答案 0 :(得分:0)

下面的演示脚本适用于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_())