EasyGUI and SimpleCV- TypeError: 'module' object is not callable

EasyGUI and SimpleCV- TypeError: 'module' object is not callable

我正在尝试为 simpleCV 程序制作 GUI。我正在使用 easyGUI 来做到这一点。 这是我的代码:

from easygui import * 
from SimpleCV import *
from cv2 import *
from cv import *
from PIL import *
import time
import sys
while True:
    msgbox("""Welcome to my program!""", image = "pi.jpg")
    msgbox("Select img ")
    nam=fileopenbox(filetypes=['*'])
    print nam
    img=Image(nam)
    img1=img.binarize()
    time.sleep(1)
    img1.save("result.png")
    msgbox("This is the result", image = "result.png")
    msg = "Do you want to continue?"
    title = "Please Confirm"
    if ccbox(msg, title): # show continue/cancle dialog
        print "okk" # user chose continue  
    else:
        sys.exit(0) # user chose cancle

但我收到奇怪的错误..它说:

Traceback (most recent call last):
  File "C:\Python27\tryyyy", line 13, in <module>
    img=Image(nam)
TypeError: 'module' object is not callable

我试过 print dir(Image),我得到:

['ADAPTIVE', 'AFFINE', 'ANTIALIAS', 'BICUBIC', 'BILINEAR', 'CONTAINER', 'CUBIC', 'DEBUG', 'EXTENSION', 'EXTENT', 'FLIP_LEFT_RIGHT', 'FLIP_TOP_BOTTOM', 'FLOYDSTEINBERG', 'ID', 'Image', 'ImageMode', 'ImagePalette', 'ImagePointHandler', 'ImageTransformHandler', 'IntType', 'LINEAR', 'MESH', 'MIME', 'MODES', 'NEAREST', 'NONE', 'NORMAL', 'OPEN', 'ORDERED', 'PERSPECTIVE', 'QUAD', 'RASTERIZE', 'ROTATE_180', 'ROTATE_270', 'ROTATE_90', 'SAVE', 'SEQUENCE', 'StringType', 'TupleType', 'UnicodeStringType', 'VERSION', 'WEB', '_E', '_ENDIAN', '_ImageCrop', '_MAPMODES', '_MODEINFO', '_MODE_CONV', 'builtins', 'doc', 'file', 'name', 'package', '_conv_type_shape', '_fromarray_typemap', '_getdecoder', '_getencoder', '_getscaleoffset', '_imaging_not_installed', '_initialized', '_show', '_showxv', '_wedge', 'blend', 'byteorder', 'composite', 'core', 'eval', 'fromarray', 'frombuffer', 'fromstring'、'getmodebandnames'、'getmodebands'、'getmodebase'、'getmodetype'、'init'、'isDirectory'、'isImageType'、'isNumberType', 'isSequenceType', 'isStringType', 'isTupleType', 'merge', 'new', 'open', 'os', 'preinit', 'register_extension', 'register_mime', 'register_open', 'register_save', 'string', 'sys', 'warnings']

我导入了SimpleCV;你能帮我为什么我会收到这个错误吗? 提前谢谢你。

正如@zoosuck 提到的,'Image' 是一个模块。这是正确的,但这不是问题所在(注意我已经编辑了这个回复)。

首先,我可以提出一些建议吗?您正试图一次编写一个完整的程序。相反,也许只尝试一个非常简单的程序,根本没有 GUI,也没有图像显示。使用 'paint' 之类的外部工具来确认您的基本逻辑是否正常运行。然后,添加漂亮的 GUI 并在基本逻辑正常工作后循环。

参考:Using python PIL to turn a RGB image into a pure black and white image

OK,回到答案:尝试回答这三个问题:

  1. 什么是图像?我不是说你认为它是什么。但是,它是什么?
  2. 它来自哪里?
  3. 为什么不是你想的那样。

花点时间尝试回答这些问题。我还想在调试中提供一个小技巧。如果你插入这两行代码,你的代码就会中断,你可以 "play around":

import code
code.interact(local=dict(globals(), **locals()))

将 'import' 放在文件的顶部。将 'code.*' 行放在您想停止的地方。 运行 程序正常。在 code.interact 语句处,您将看到 >>> 出现。现在,玩吧!输入 print Image 或 print(Image),试试 help(Image)。你得到了什么?这是我得到的:

>>> Image
<module 'PIL.Image' from 'C:\Python27\lib\site-packages\PIL\Image.pyc'>

等等!它说 PIL.Image 这不是 SimpleCV!问题出在您的导入语句上。我建议你改变它们。你可以照你现在做的去做,但有时会很混乱。相反,将它们更改为:

import SimpleCV as cv
import PIL as pil

然后,在您的代码中,总是说 'cv.Image',例如。因为你不是很具体而且因为 PIL 覆盖了 'Image',所以你弄错了。而是像我上面所说的那样导入,我认为它会减少很多混乱。希望对您有所帮助!