2014-03-13 64 views
0

我是新的編程接口。我正在用wxpython和openCV創建一個簡單的界面來打開一個圖像,保存並關閉界面。你可以在下面看到我的代碼。我可以打開一張圖片並關閉界面。甚至,我爲開放和保存例程顯示對話框,但保存是我遇到問題的地方。我不知道如何發送到保存img(圖像對象)來保存。這點我不清楚。你可以幫我嗎?提前致謝。全球名稱'img'未定義?

import wx 
import cv2 

class MyMenu(wx.Frame): 
def __init__(self, parent, id, title): 
    wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(200, 150)) 
    menubar = wx.MenuBar() 
    file = wx.Menu() 
    edit = wx.Menu() 
    help = wx.Menu() 
    file.Append(101, '&Open', 'Open a new document') 
    file.Append(102, '&Save', 'Save the document') 
    file.AppendSeparator() 
    quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the Application') 
    file.AppendItem(quit) 

    menubar.Append(file, '&File') 
    menubar.Append(edit, '&Edit') 
    menubar.Append(help, '&Help') 

    self.SetMenuBar(menubar) 
    self.CreateStatusBar() 

    self.Bind(wx.EVT_MENU, self.OnOpen, id=101) 
    self.Bind(wx.EVT_MENU, self.OnSave, id=102) 
    self.Bind(wx.EVT_MENU, self.OnQuit, id=105) 

def OnOpen(self, event): 
    openFileDialog = wx.FileDialog(self, "Open", "", "", 
            "PNG files (*.png)|*.png|BMP files (*.bmp)|*.bmp", 
            wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) 
    openFileDialog.ShowModal() 
    path = openFileDialog.GetPath() 
    openFileDialog.Destroy() 
    img = cv2.imread(str(path)) 
    cv2.imshow('img', img) 
    return img 

def OnSave(self, event): 
    saveFileDialog = wx.FileDialog(self, "Save As", "", "", 
            "PNG files (*.png)|*.png|BMP files (*.bmp)|*.bmp", 
            wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) 
    saveFileDialog.ShowModal() 
    path_save = saveFileDialog.GetPath() 
    print path_save 
    saveFileDialog.Destroy() 
    cv2.imwrite(str(path_save), img) 

def OnQuit(self, event): 
    self.Close() 

class MyApp(wx.App): 
    def OnInit(self): 
     frame = MyMenu(None, -1, 'menu1.py') 
     frame.Show(True) 
     return True 

app = MyApp(0) 
app.MainLoop() 

我收到以下錯誤:

NameError:全局名稱 'IMG' 沒有定義

EDIT(最終版):

import wx 
import cv2 
import numpy as np 


class MyMenu(wx.Frame): 
def __init__(self, parent, id, title): 
    wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(200, 150)) 
    img = np.array([0]) 
    menubar = wx.MenuBar() 
    file = wx.Menu() 
    edit = wx.Menu() 
    help = wx.Menu() 
    file.Append(101, '&Open', 'Open a new document') 
    file.Append(102, '&Save', 'Save the document') 
    file.AppendSeparator() 
    quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the Application') 
    file.AppendItem(quit) 

    menubar.Append(file, '&File') 
    menubar.Append(edit, '&Edit') 
    menubar.Append(help, '&Help') 

    self.SetMenuBar(menubar) 
    self.CreateStatusBar() 

    self.Bind(wx.EVT_MENU, self.OnOpen, id=101) 
    self.Bind(wx.EVT_MENU, self.OnSave, id=102) 
    self.Bind(wx.EVT_MENU, self.OnQuit, id=105) 

def OnOpen(self, event): 
    openFileDialog = wx.FileDialog(self, "Open", "", "", 
            "PNG files (*.png)|*.png|BMP files (*.bmp)|*.bmp", 
            wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) 
    openFileDialog.ShowModal() 
    path = openFileDialog.GetPath() 
    openFileDialog.Destroy() 
    self.img = cv2.imread(str(path)) 
    cv2.imshow('img', self.img) 

def OnSave(self, event): 
    saveFileDialog = wx.FileDialog(self, "Save As", "", "", 
            "PNG files (*.png)|*.png|BMP files (*.bmp)|*.bmp", 
            wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) 
    saveFileDialog.ShowModal() 
    path_save = saveFileDialog.GetPath() 
    print path_save 
    saveFileDialog.Destroy() 
    cv2.imwrite(str(path_save), self.img) 

def OnQuit(self, event): 
    self.Close() 


class MyApp(wx.App): 
def OnInit(self): 
    frame = MyMenu(None, -1, 'menu1.py') 
    frame.Show(True) 
    return True 

app = MyApp(0) 
app.MainLoop() 
+1

什麼行!!!!!!! –

+0

對不起,它說:Traceback(最近呼叫最後一個): 第47行,OnSave cv2.imwrite(str(path_save),img) NameError:全局名稱'img'未定義 – user3416588

回答

1

看它,你的意思是img用於OnSave,如果你看,img沒有在範圍內定義,也不是全球定義。

+0

釘住它!在錯誤中找到了該行! –

+0

好的,我必須在全球範圍內定義它?在我的MyMenu類之前,我嘗試了像這樣的'img = numpy.array([0],[0])',然後我打開了一個圖像,當我試圖保存它時,這次沒有錯誤,但保存的圖像是空的。我想打開一張圖片,修改它然後保存。 – user3416588

+0

這是因爲wxWidgets期望我會想象一個wxImage。你需要閱讀文檔。 –