python use opencv get an error - python

When I use the opencv with the python, I always get an error.
I set the environment like this:
install the python "python-2.7.5.msi"
install the numpy "numpy-MKL-1.7.1.win32-py2.7.exe"
install the opencv "opencv-python-2.4.6.win32-py2.7.exe"
Everything is OK. I test it using the following code:
import cv2
img = cv2.imread('lena.bmp')
cv2.show('Image', img)
cv2.waitKey(0)
But I got an error as follow:
File "E:\Python\cv2.py", line 1, in <module>
import cv2
File "E:\Python\cv2.py", line 2, in <module>
img = cv2.imread('lena.bmp')
AttributeError: 'module' object has no attribute 'imread'
Why? But when I print "import cv2" in the IDLE, I didn't get any error.

well, I guess you imported your file, ie the file you are writing. cause the file is using the name "cv2.py" as it suggested. you`d better change the filename.

Related

How to resolve ImportError?

I have written a python code in which I am using opencv-python. The code is perfectly running on my local machine. I am trying to make it live by uploading it on a server.
File "/var/www/xxxx/xxxxx/xxxxx/imgmask.py", line 2, in
import cv2 as cv
from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
How can I resolve this problem? Please help!!
Note: I am using windows 10.
In the file you are importing cv2 copy the following code and paste and run it.
try:
import cv2
except Exception as e:
from pip._internal import main
main(["install", "opencv-python"])
finally:
pass
Good Luck, let me know if it works
If you use pipenv, you should write on terminal: pip install opencv-python

Python script using PIL works in IDE doesn't when run by cmd and gives 'PIL' has no attribute 'Image'

When I run the following in the Spyder IDE on Win10:
import PIL
import os
Inboundfile = "C:/Test/test/Overlay.png"
image = PIL.Image.open(os.path.abspath(Inboundfile))
image.show()
It works fine. Opens the picture and outputs:
runfile('C:/Users/bob/Anaconda3/envs/NewEnvironment/bob/test3.py', wdir='C:/Users/bob/Anaconda3/envs/NewEnvironment/bob')
But when I call the same script from a cmd prompt:
C:\Users\bob\Anaconda3\envs\NewEnvironment\bob>python test3.py
It results in:
Traceback (most recent call last):
File "test3.py", line 14, in <module>
image = PIL.Image.open(os.path.abspath(Inboundfile))
AttributeError: module 'PIL' has no attribute 'Image'
I have verified that the environment path is pointing to the same python environment as the IDE. ( I think ) Here is my Path variable from OS
C:\Users\bob\Anaconda3\envs\NewEnvironment\
It's probably something very simple but I'm very new at this and any help would be appreciated.
I tried adding the line:
from PIL import Image
It still runs fine in the IDE but the error message changes to:
C:\Users\bb\Anaconda3\envs\NewEnvironment\bob>python test3.py
Traceback (most recent call last):
File "test3.py", line 11, in <module>
from PIL import Image
File "C:\Users\bob\Anaconda3\envs\NewEnvironment\lib\site-packages\PIL\Image.py", line 69, in <module>
from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.
This is even more confusing as I can follow that path and see Image.py sitting in the directory. What is the IDE doing that CMD is not?
All of my problems vanished when I simply closed and restarted the console I was using to call the python script. I have learned that the console saves things like the path variables when it starts up and must be restarted to get changes.

RaspberryPi: ImportError: No module named cv2 in Geany

I installed opencv in RaspberryPi (using this link)and import in termial and python3 (idle). It shows no error in these two as shown in below images.
But, when I tried to run import cv2 in Geany, it shows error given below:
Traceback (most recent call last):
File "import.py", line 1, in <module>
import cv2
ImportError: No module named cv2
How I can solve this problem?
I solved this problem by changing python to python3 in Geany.
For this, Go to Build->Set Build Commands in Geany menu. In Set Build Commandswindow, under Python commands label in compile line python m py_compile will be there. Similarly, under Executive commands label in executive line python "%f" will be there. I changed python m py_compile into python3 m py_compile and python "%f" into python3 "%f" (as shown in images 1 and 2) and above-mentioned problem get solved.
Image 1.
Image2.
Thanks.

Python - Cannot import name - 3.4

I am working with numpy and Pillow (replacement for PIL in 3.4), but have been having problems with the importing of Pillow. I found a similar post here:
ImportError: Cannot import name X
However, this post was using his own created libraries and the problem was that his modules imported each other, creating circular dependent imports.
My code however does not use my own module, here is the code:
import PIL
from PIL import ImageGrab
import numpy
img = ImageGrab.grab()
imgLoad = img.load()
size = img.size()
And this then returns the error:
Traceback (most recent call last):
File "E:/Family Documents/Matthew's Documents/Python/PIL.py", line 1, in <module>
import PIL
File "E:/Family Documents/Matthew's Documents/Python\PIL.py", line 2, in <module>
from PIL import ImageGrab
ImportError: cannot import name 'ImageGrab'
One other interesting thing about this is that when I first installed Pillow (PIL), I tried it in the shell and the "from PIL import ImageGrab" worked.
Also, if I restart the shell (close it and re-open it) the commands, being manually typed in, work as well. This suggests to me that something is bugging out with python, since retyping "import PIL" throws the same error message "cannot import name 'ImageGrab'".
Thanks for any help
Ha, this already bit me multiple times.
Your traceback shows the file name:
E:/Family Documents/Matthew's Documents/Python/PIL.py
Your PIL.py is found first, so you're trying to import names from the module that's just executing, not from the actual library you have installed.

have trouble with PIL to create PhotoImage object

I have Python 2.6 running on Fedora 13. I installed PIL 1.1.7, and I get the successful installed message in the Python prompt. I am able to import PIL.PhotoImage,but when I try to run the following, I get an error.
mgobj = PhotoImage(file=imgpath)
Stacktrace:
Traceback (most recent call last):
File "viewer-tk.py", line 25, in <module>
imgobj = PhotoImage(file=imgpath) # now JPEGs work!
File "/home/Toshiba/vinpython/venv/lib/python2.6/site-packages/PIL/ImageTk.py", line 116, in __init__
self.paste(image)
File "/home/Toshiba/vinpython/venv/lib/python2.6/site-packages/PIL/ImageTk.py", line 181, in paste
import _imagingtk
ImportError: No module named _imagingtk
In the module ImageTk.py , I see _imagingtk being imported,but i am not sure how do it get that module.Help is greatly appreciated!!!
You need to install Tkinter python module as stated at this trac. But since it's an integral part of python distributions. Since you are using fedora take a look at this page where you can install Tkinter when it doesn't come along, even being for Fedora 3 I think it might help.

Categories