这是场景。首先,我想将图像上传到GUI。稍后按下另一个按钮,我想处理这个上传的图像。现在,我能够在GUI中显示图像。但我对如何获取此图像的名称一无所知,因此我按下另一个按钮处理此图像。现在这是我的代码:
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
import sys
import cv2
from sklearn.externals import joblib
from sklearn.svm import LinearSVC
from skimage.feature import hog
import numpy as np
import os.path
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(100, 100, 1000,600)
self.setWindowTitle("HandWritten Image Recognition")
extractAction = QtGui.QAction("&Quit", self)
extractAction.setShortcut("Ctrl+Q")
extractAction.setStatusTip("Leave the App")
extractAction.triggered.connect(self.close_application)
openFile = QtGui.QAction("&Open File", self)
openFile.setShortcut("Ctrl+O")
openFile.setStatusTip("Open File")
openFile.triggered.connect(self.file_open)
processImage = QtGui.QAction("&Process Image", self)
processImage.setShortcut("Ctrl+A")
processImage.setStatusTip("Process Image")
processImage.triggered.connect(self.process_image)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(processImage)
fileMenu.addAction(extractAction)
def file_open(self):
# This function is called when the user clicks File->Open.
filename = QtGui.QFileDialog.getOpenFileName()
print(filename)
label = QtGui.QLabel(self)
label.setGeometry(QtCore.QRect(0, 20, 1000, 580))
label.setSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
label.setScaledContents(True)
pixmap = QtGui.QPixmap(filename)
label.setPixmap(pixmap)
label.show()
def process_image(self):
###Get Uploaded Image's Name
def close_application(self):
print("Why so hurry!!!")
sys.exit()
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())
现在,在process_image函数中,我想使用上传的图像,所以我可以进一步处理它。所以,为此,我想知道上传图像的文件路径。请在这里指导我。
答案 0 :(得分:1)
这很简单。请将filename
设置为self
对象的变量image
。请将以下代码添加到您的函数process_image
:
os.path.basename(str(self.image))
请在此处找到更新的代码:
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
import sys
import cv2
from sklearn.externals import joblib
from sklearn.svm import LinearSVC
from skimage.feature import hog
import numpy as np
import os.path
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(100, 100, 1000,600)
self.setWindowTitle("HandWritten Image Recognition")
extractAction = QtGui.QAction("&Quit", self)
extractAction.setShortcut("Ctrl+Q")
extractAction.setStatusTip("Leave the App")
extractAction.triggered.connect(self.close_application)
openFile = QtGui.QAction("&Open File", self)
openFile.setShortcut("Ctrl+O")
openFile.setStatusTip("Open File")
openFile.triggered.connect(self.file_open)
processImage = QtGui.QAction("&Process Image", self)
processImage.setShortcut("Ctrl+A")
processImage.setStatusTip("Process Image")
processImage.triggered.connect(self.process_image)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(processImage)
fileMenu.addAction(extractAction)
def file_open(self):
# This function is called when the user clicks File->Open.
filename = QtGui.QFileDialog.getOpenFileName()
print(filename)
self.image = filename #Assign filename to object's variable 'image'
label = QtGui.QLabel(self)
label.setGeometry(QtCore.QRect(0, 20, 1000, 580))
label.setSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
label.setScaledContents(True)
pixmap = QtGui.QPixmap(filename)
label.setPixmap(pixmap)
label.show()
def process_image(self):
print os.path.basename(str(self.image)) #Get only file name with it's extension
def close_application(self):
print("Why so hurry!!!")
sys.exit()
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())