Python 2.7从非默认目录中打开多个文件(对于opencv) [英] Python 2.7 opening multiple files from non-default directory (for opencv)
问题描述
我在64位win7上使用python 2.7,并且具有opencv 2.4.x.
I'm using python 2.7 on 64 bit win7 and have opencv 2.4.x.
当我编写cv2.imread('pic')时,它将在我的默认python路径C:\Users\Myname
中打开pic.但是,我将如何设法浏览不同的目录以打开图像?即D:\MyPicLib
.
When I write cv2.imread('pic') it opens pic in my default python path which is C:\Users\Myname
. But how I will manage to browse different directory to open images? ie D:\MyPicLib
.
同时,我不想更改默认目录,因为我所有的python模块都保存在C:\Users\Myname
中. 我只想管理<c1>
Meanwhile, I do not want to change default directory, because all my python modules are saved in C:\Users\Myname
. I just want to manage connecting pictures in D:\MyPicLib
在这部分之后,您还可以帮助我在for/while循环中浏览多个图像(特定格式,例如目录中的.jpg)而不是一个图像吗?
After this part, can you also help me to browse not one but multiple images (in specific format, like just .jpg's in a directory) in for/while loop?
预先感谢您,问题似乎很简单,但是尽管我付出了所有努力,但我没有找到任何更改默认python路径的解决方案.
Thank you in advance, the problem seems easy but despite all my effort, I did not find any solution on changing default python path.
推荐答案
这是您要找的东西吗?我建议寻找一些显示基本os和os.path用法的教程.它们是非常有用的工具. 这里是我找到的第一个.
Is this what you are looking for? I recommend looking for some tutorials that show basic os and os.path usage. They are very useful tools. Here is the first one I found.
import os
import cv2
mypath = os.path.join('c:\\', 'asdf', 'jkl')
images = list()
for item in os.listdir(mypath):
if '.jpg' in item: # this could be more correctly done with os.path.splitext
image = cv2.imread(os.path.join(mypath, item))
if image is not None:
images.append(image)
这篇关于Python 2.7从非默认目录中打开多个文件(对于opencv)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!