1

I started to learn opencv with Python, and right away i get an error. Im trying for an hour to find answer but I failing.

I want to read the image thats saved on my desktop, and py .py file is also saved on deskop.

This is my code:

import cv2

my_image = cv2.imread('slika.png', 0)

And I get this error:

AttributeError: module 'cv2' has no attribute 'imread'

I have installed open cv lib like this:

pip install opencv-python

When I do:

print(dir(cv2))

I get this:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'cv2']

And this:

print(cv2.__file__)

#C:\Users\nenad\OneDrive\Desktop\cv2.py

Im using windows, Python 3.7 32-bit and IDLE that came with python (basic one) I know that this is basic, but what am i doing wrong?

taga
  • 3,537
  • 13
  • 53
  • 119
  • Depending on how you are set up, you may need to use `pip3 install opencv-python` if your interpreter is Python3. Using plain `pip install XYZ` may be installing for Python2. You should state which IDE/editor you are using, if any. Also, is that your complete code, or have you removed other stuff? – Mark Setchell Sep 13 '19 at 09:03
  • Im using IDLE that came with python (basic one), I installed all my libs with pip, not pip3 and they work fine. When Im using ` Python -m pip3 install opencv-python` i get `SyntaxError: invalid syntax` – taga Sep 13 '19 at 09:09
  • Ok, please `import cv2` and then `print(dir(cv2))` and paste the output into your question - not the comment area, please. – Mark Setchell Sep 13 '19 at 09:11
  • Check out the question, I have updated it (I have done what you told me) – taga Sep 13 '19 at 09:13
  • Ok, and the output of `print(cv2.__file__)` ? – Mark Setchell Sep 13 '19 at 09:16
  • I have updated my answer – taga Sep 13 '19 at 09:21
  • 3
    You're importing a local file that is on your desktop called `cv2.py`. Get rid of that. Otherwise, your code is importing THAT instead of OpenCV (because Python will look in local directories first, THEN it will search your python path for libs). – alkasm Sep 13 '19 at 09:30
  • 1
    You need to rename your Python script as say `opencvtest.py` else you are conflicting with the OpenCV package itself. – Mark Setchell Sep 13 '19 at 09:32
  • @alkasm please post that as answer so that I can confirm the answer :) – taga Sep 13 '19 at 09:37
  • 1
    Probably unlikely to help others as a question since it's more akin to a typo, so I'd recommend deleting it, or finding a good duplicate that isn't specific to opencv and marking as a dupe. – alkasm Sep 13 '19 at 09:39

0 Answers0