3

A similar question to mine exists, however it does not answer my question.

Here is what I am working with:

Python v. 3.6.2
opencv 1.0.1
PyCharm Community Edition 2017 .2.2
macOS Sierra Version 10.12.6

I'm trying to use imread for image processing. I've looked at the documentation and I am using the function correctly. Here is the test code that comes with the opencv library:

import opencv img = cv.imread('background.png') if img is None: print("Image not loaded!") else: print("Image is loaded!") 

I can see my python files and and modules in the project explorer. When I run the code, I get the following error:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/lmc/Desktop/pywerk/opencvpractice Traceback (most recent call last): File "/Users/lmc/Desktop/pywerk/opencvpractice", line 4, in img = cv.imread('background.png') AttributeError: module 'opencv' has no attribute 'imread'

I've tried everything from reinstalling python and the opencv module to switching python versions to 2.7 (and using the respective opencv module) and I get the same error.

Is there some sort of system configuration I could be missing? Any help would be much appreciated.

2
  • 3
    Any particular reason you are using the deprecated OpenCV 1.x instead of OpenCV 2.x or 3.x with the cv2 module? The cv2 module uses a numpy backend for image storing so it's far superior for working with the data. Anyways, the answer below should fix you up; you're importing opencv yet using the namespace cv which you haven't defined. Commented Aug 29, 2017 at 2:35
  • 1
    OpenCV 1.0.1? No such version was released, but even it it was, it would predate Python 3.0 by about 2 years. Seems like some nonsense... perhaps this bit of junk in the package index. Commented Aug 29, 2017 at 9:29

3 Answers 3

1

maybe you should try with opencv.imread?

import opencv img = opencv.imread('background.png') if img is None: print("Image not loaded!") else: print("Image is loaded!") 

or alternatively import opencv as cv:

import opencv as cv img = cv.imread('background.png') if img is None: print("Image not loaded!") else: print("Image is loaded!") 
Sign up to request clarification or add additional context in comments.

Comments

1

For OpenCV, it should be imported as

import cv 

or import cv2 (If you want to change to opencv V2.x or 3.x)

Comments

1

Turns out it was a combination of several of these suggestions; if I could give the answer props to Alexander Reynolds that would be the most accurate. I was following an outdated tutorial and ended up with an outdated version of opencv. I downloaded opencv using the instructions here, for anyone else who is looking for the exact commands:

https://pypi.python.org/pypi/opencv-python/3.1.0.3

Here is what I ended up with:

import cv2 img = cv2.imread('background.png') if img is None: print("Image not loaded!") else: print("Image is loaded!") 

Thanks for the help!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.