Python import cv2 recognised but doesn't work - python

I use windows and Eclipse as IDE. I have installed opencv-python and numpy with pip.
When I try to do:
import cv2
It seems that it is recognised, but I'm not able to use not even a method of this library.
In "C:\Python27\Lib\site-packages" I have both:
opencv_python-3.2.0.7.dist-info
cv2
as folders.
In cv2 i have:
__init__
__init__
cv2.pyd
opencv_ffmpeg320_64.dll
I don't understand what's wrong, the import works but then methods are not recognised.
For example:
import cv2
image = cv2.imread('tmp.png')
cv2.imwrite("tmp.jpg", image)
"imread" and "imwrite" are said to be "undefined variable from import cv2".
Any suggest?
Thank you in advance,
Antonio

Related

python module that imports another python module

I'm new to python and I'm having an issue importing a module that imports numpy,PIL and os packages. I'll try and be as clear as possible with my problem
So I have a module lets call it preprocessing.py in which I've written a class to process an image imported from PIL using Image and converting it to a numpy array so the structure looks like the following (note method1 converts a jpg to numpy array)
----- preprocessing.py
import numpy as np
import os
from PIL import Image
Class process_object:
method1
Now I want to use this module as follows I want to import process_object from preprocessing.py and use method1 to process an image again imported using Image in PIL. So my script computation.py looks like the following
---computation.py
import os
import numpy as np
from PIL import Image
a = process_image(input)
a.method1()
However, when I do this I get the following error message
ImportError: No module named numpy
Could someone explain to me what is going on and how to fix it? I'd really appreciate an explanation which allows me to understand what is going on under the hood, so I can avoid situations like this. I really appreciate any help! Thanks!!
Check in which version of Python pip is installing numpy. It could be that when pip installs it, it's pointing to a different Python version on your system.
For problems like these, I would recommend using:
https://github.com/pyenv/pyenv-virtualenv
Will handle Python versions for you, so that you can differentiate which packages are being installed.
I will also recommend using PyCharm's Community Edition.
https://www.jetbrains.com/pycharm/download
Excellent tool and lets you create your own environment.
Hope this helps.
https://sourceforge.net/projects/numpy/files//NumPy/1.5.0/NOTES.txt/view. This is the support for numpy in Python 3.0. You probably need a newer version of numpy. You can also use:
pip install numpy
or
pip3 install numpy

Importing OpenCV - import cv2 or cv3, and why does the directory matter?

I am trying to use OpenCV for Python 3 for the first time. To install, I simply typed "pip3 install opencv-python" into the terminal. When I do this, I see in Finder (I'm on a Mac), that in /usr/local/lib/python3.7/site-packages, I now have a new folder called "cv2". This is strange. Why isn't it cv3, as I used pip3? I do not know.
When I try to import cv2 from python3, it says "No module named 'cv2'". Luckily StackOverflow already has a solution: if I first change directories like: "cd /usr/local/lib/python3.7/site-packages" then I will be able to open cv2. I do not know why this step is necessary for OpenCV. I don't have to do this to "import numpy" for example; "numpy" does not care what directory I start from, and it is listed like just another module like cv2.
Anyways, I change directory to site-packages, start python3, and I import cv2. Now there is a new error. This time cv2 is found, but it says:
"from .cv2 import *
ModuleNotFoundError: No module named 'cv2.cv2'"
For the record, I also have Python 2.7. If I try "cd /usr/local/lib/python2.7/site-packages" and then start Python 2.7, I can import cv2. This is ok, but I want to use OpenCV with Python3.
Let me summarize.
(1) How can I get cv2 or cv3 to import when I start from any directory? Is this ever possible?
(2) How can I get cv3 for python3? cv2 seems to be problematic in python3 because because there is no "cv2.cv2"
The only thing we need to know here is, that 2 in cv2 is not a version. cv2 uses API of C++ while cv uses C API. Version of it is constantly updating being inside CV2.
There is no cv3 there's only import cv2 that opencv older or latest that is updated for old python or latest.

I am not able to import an image into python using the import image way

Before you ask, I did try putting an image in it, and it didn't work like look at this.
import Image
Image = Image.open('clerky.jpeg')
Image.show()
and this code above comes up with this error below.
import Image
ImportError: No module named 'Image'
If you're using Pillow, the correct usage is
from PIL import Image
To install Pillow on Windows, start the Command Prompt application (or hit WinR and type cmd, then hit Enter). Type
pip install Pillow
hit Enter, and everything should install automatically.
I think you mean from PIL import Image
from PIL import Image
PIL (Python Image Library) has to be installed on your system, AKA "pil" and "Pillow" from time to time. effbot.org/imagingbook/pil-index.htm

Using Python Imaging Library (PIL) in Eclipse

I am attempting to use PIL in Eclipse. I have managed to install the following packages so that this code runs without error:
from PIL import Image
import os
from pylab import *
from scipy.misc import *
im = array(Image.open('empire.jpg').convert('L'))
However, the following:
imshow(im)
Produces the error:
raise RuntimeError('Could not execute image viewer.')
RuntimeError: Could not execute image viewer.
I tried to install the basic Image Viewer from the Eclipse Marketplace. It doesn't solve the problem.
Does anybody know how to get Eclipse display the image?
Thanks in advance.
This ran OK for me (Windows, Python2.6), with the following two changes:
commented out the unused scipy import
added show() at the end
pl.show()
works.
If your import like this:
import matplotlib.pyplot as pl

Python Image Library installation

I just installed the 1.15 Python Image Library in the following folder:
C:\Python26\Lib\site-packages\
However, when I write
from PIL import Image, ImageOps, ImageDraw
I get a error saying unresolved import. Thanks for the help in advance!
The actual modules are just Image, ImageOps, ImageDraw; the PIL package is something one particular distro decided to tack on because they thought those module names were too generic, but they're not actually found in PIL.

Categories