全局名称 'img' 未定义?

0 投票
1 回答
2919 浏览
提问于 2025-04-17 22:10

我刚开始学习编程接口。我正在用wxpython和openCV创建一个简单的界面,功能是打开一张图片、保存它,然后关闭界面。你可以看到我下面的代码。我可以打开图片并关闭界面,甚至在打开和保存的时候显示对话框,但在保存的时候我遇到了问题。我不知道怎么把要保存的图片对象(img)传给OnSave这个函数。这一点我不太明白。你能帮我吗?谢谢!

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: global name 'img' is not defined

编辑(最终版本):

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 个回答

1

你说的是在 OnSave 里用到的 img,如果你仔细看看,会发现 img 并没有在这个范围内定义,也没有在全局范围内定义。

撰写回答