简体   繁体   English

从Python模块内sys.path中的路径读取图像

[英]Read image from a path in sys.path inside a Python module

I am very new to python and opencv world. 我对python和opencv世界很陌生。 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 错误:D:\\ Build \\ OpenCV \\ opencv-3.1.0 \\ modules \\ highgui \\ src \\ window.cpp:289:错误:(-215)size.width> 0 && size.height> 0在函数cv :: imshow中

I have created a module named test.py. 我创建了一个名为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: 在该模块中,我尝试读取系统路径“ C:\\ cv \\ images”中的图像“ car.png” ,并将其显示如下:

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. 当我调试代码时,我可以看到im变量从未初始化,这就是为什么我得到该错误代码的原因。 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. 但是,当我在解释器中键入sys.path时,它表明该路径已经被添加了很多次,我试图运行我的模块。 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. 似乎在模块内部,未考虑sys.path,并且python无法读取图像。

Any idea if this is a normal behavior, or should I do something inside my module to let the interpreter read sys.path contents? 您是否知道这是正常现象,还是应该在模块内部做一些事情,让解释器读取sys.path内容?

What makes you imagine that sys.path settings will affect the directories from which you read files? 是什么让您想象sys.path设置会影响您从中读取文件的目录? It's purely used to locate Python modules for import. 它仅用于查找要导入的Python模块。 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 如果您在dir有一个目录,在filename中有一个文件filename那么您应该打开的文件将是

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. 那些对使用Python软件包运送数据文件感兴趣的人,可能比看pkgutil软件包的文档pkgutil In particular, pkgutil.get_data is useful for finding data files wherever the installation process has put them. 特别是pkgutil.get_data对于在安装过程中放置​​数据文件的地方查找数据文件很有用。

I'm not sure what you're trying to do in your application but sys.path seems superfluous in the example code. 我不确定您要在应用程序中做什么,但是sys.path在示例代码中似乎是多余的。 Furthermore, sys.path says in the Python documentation that it is 此外, sys.pathPython文档中说它是

A list of strings that specifies the search path for modules 字符串列表,指定模块的搜索路径

Basically, sys.path is for loading modules themselves not files inside of modules. 基本上, sys.path用于加载模块本身,而不是模块内部的文件。

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: 此变通办法会蛮力地压缩sys.path直到加载文件,该方法可正常工作,但不是优雅,常规或必要的:

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 : 在查看了您所使用的Open CV版本的源代码内部加载函数之后 ,似乎imread函数并未考虑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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM