【问题标题】:Global name 'img' is not defined?未定义全局名称“img”?
【发布时间】:2014-04-18 15:30:33
【问题描述】:

我是新的编程接口。我正在使用 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:未定义全局名称“img”

编辑(最终版本):

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

【问题讨论】:

  • 对不起,上面写着: Traceback (last recent call last): line 47, in OnSave cv2.imwrite(str(path_save), img) NameError: global name 'img' is not defined

标签: python opencv interface wxpython


【解决方案1】:

看你的意思是OnSave中使用的img,如果你看,img没有定义在范围内,也没有全局定义。

【讨论】:

  • 好的,我必须在哪里全局定义它?我在 MyMenu 课之前尝试过类似img = numpy.array([0],[0]) 的操作,然后我打开了一张图片,当我尝试保存它时,这次没有错误,但保存的图片是空的。我想打开一张图片,修改它然后保存。
  • 那是因为 wxWidgets 需要一个我可以想象的 wxImage。您需要阅读文档。
猜你喜欢
  • 2018-05-04
  • 2015-12-22
  • 2015-08-08
  • 2011-04-27
  • 2013-09-04
  • 2015-02-26
  • 2016-09-11
  • 1970-01-01
相关资源
最近更新 更多