0

I am very new to python and opencv world. I was just trying to read and show an image but I always get an error:

error: D:\Build\OpenCV\opencv-3.1.0\modules\highgui\src\window.cpp:289: error: (-215) size.width>0 && size.height>0 in function cv::imshow

I have created a module named test.py. In that module, I tried to read the image "car.png" which is in my system path "C:\cv\images" and show it as below:

import cv2;
import sys;


sys.path.append('C:\\cv\\images');

im = cv2.imread('car.png');
cv2.imshow('Car Figure',im);
cv2.waitKey(0);

When I debug the code, I can see that the im variable is never initialized which is why I get that error code. However, When I type sys.path in the interpreter it shows that the path was already added as many times I tried to run my module. And when I copy/paste the module contents directly in the interpreter, the code works fine and the image appears.

It seems that inside the module, the sys.path is not taken into consideration and python is unable to read the image.

Any idea if this is a normal behavior, or should I do something inside my module to let the interpreter read sys.path contents?

2 Answers 2

2

What makes you imagine that sys.path settings will affect the directories from which you read files? It's purely used to locate Python modules for import. So to answer your question, the behaviour you are seeing is normal and expected. If you have a directory in dir and a filename in filename then the file you should be opening will be

os.path.join(dir, filename)

so you should try

im = cv2.imread(os.path.join(dir, filename))

Those interested in shipping data files with their Python package could do worse than take a look at the documentation for the pkgutil package. In particular, pkgutil.get_data is useful for finding data files wherever the installation process has put them.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. But, lets say I want to make my module portable. I need to know how to reference a path relative the current module path instead of typing a full path name. How can I do that?
@HishamRagheb if you go to google and search "relative path names" you'll find a wealth of examples
Most modules will have a __file__ attribute, from which you can extract the directory path with os.path.dirname(module.__file__), then join it to the name of the file you want to read.
@holdenweb Thanks. That what I was looking for
thank you for the answer, I was struggling with sys.path.append() but it did not solve my problem, but your answer really helped me
1

I'm not sure what you're trying to do in your application but sys.path seems superfluous in the example code. Furthermore, sys.path says in the Python documentation that it is

A list of strings that specifies the search path for modules

Basically, sys.path is for loading modules themselves not files inside of modules.

The variable is never initialized because it isn't loading the image. This workaround, which bruteforces the sys.path until a file loads, works fine but is not elegant, conventional, or necessary:

import sys
import cv2

sys.path.append('C:\\users\\xxxx\\pictures\\')

loaded = False
for rel in sys.path:
    im = cv2.imread(rel+'image.jpg')
    if im is not None:
        loaded = True
        cv2.imshow('Car Figure',im)
        cv2.waitKey(0)

if loaded == False:
    raise Exception('Couldn\'t load image')

After looking at the source code and internal load function for the version of Open CV you're using, it seems that the imread function does not consider sys.path:

Mat imread( const String& filename, int flags )
{
    /// create the basic container
    Mat img;

    /// load the data
    imread_( filename, flags, LOAD_MAT, &img );

    /// return a reference to the data
    return img;
}

3 Comments

Still same error. As I mentioned If I print sys.path from inside the module. I can see the path 'C:\\cv\\images' is already in sys.path list. But imread can't see the file 'car.png'
@HishamRagheb I updated my code snippet above to solve that issue. What I'm saying is that imread is not aware of sys.path and will not be aware of it unless you modify the source code and recompile Open CV yourself.
Thank you for the valuable info. Your solution looks great but will require a compilation of opencv while I'm still crawling with python from a java world prespective. I used a simpler hint from @holdenweb which is os.path.dirname(sys.argv[0])+'\\images' to reference my images folder relatively and that was enough for now. Thank you again. Wanted to vote up but my reputation is still low to do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.