Python + opencv with PyCharm- 'opencv' has no attribute 'imread'

5.1k views Asked by At

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.

3

There are 3 answers

0
lane On BEST ANSWER

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!

0
Totoro On

For OpenCV, it should be imported as

import cv

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

0
Reblochon Masque On

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!")