Search code examples
python-2.7simplecveasygui

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


I am trying to make GUI for simpleCV program. I am using easyGUI to do that. Here is my code:

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

But I am getting weird error.. it says:

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

I tried print dir(Image), and I got:

['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']

I imported the SimpleCV; can you help me why I am getting this error, please? Thank you in advance.


Solution

  • As @zoosuck mentioned, 'Image' is a module. This is correct, but it isn't the problem (NOTE I've edited this response).

    first, might I suggest something? You are trying to write a full program all at once. Instead maybe try just a very simple program with no GUI at all and no display of the image. Use an external tool like 'paint' to confirm that your base logic functions properly. THEN, add in the nice GUI and loop once you get the base logic working properly.

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

    OK, back to the answer: Try to answer these three questions:

    1. What is Image? I don't mean what do you think it is. But, what is it?
    2. Where does it come from?
    3. Why isn't it what you thought it was.

    Take a moment to try to answer these. I also wanted to give a small tip in debugging. If you insert these two lines of code you will get a break in your code where you can "play around":

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

    Put that 'import' at the top of your file. Put the 'code.*' line somewhere where you want to stop. Run the program as normal. At the code.interact statement, you will see >>> appear. Now, play around! type print Image or print(Image), try help(Image). What do you get? Here is what I got:

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

    Wait! It says PIL.Image which is NOT SimpleCV! The problem is with your import statements. I would suggest that you change them. You can do as you are doing, but it is very confusing sometimes. Instead, change them to something like:

    import SimpleCV as cv
    import PIL as pil
    

    Then, in your code, always say 'cv.Image', for instance. Because you weren't very specific and because PIL overwrote 'Image', you got the wrong one. Instead import as I stated above and I think it will be a lot less confusing. I hope this helps!