Reading images in python using OpenCV and OS libraries

假装没事ソ 提交于 2021-02-08 11:58:50

问题


I have a folder with numerous images(about 300), I am gonna save the python file which will be splitting the images into red, green and blue channels and saving them as _red, _green, _blue, preceded by the original image name itself in a different folder. For example if the image is named "image 001", then the images obtained after the split are: "image 001_red", "image 001_green", "image 001_blue". Now, is there a way I can obtain the images one after the other using the OS library? (Appreciate any answer what-so-ever, because this is my first question on this site)


回答1:


You are asking how to read an image list from a directory to python. Here is how.

from os import walk

# Get file list
def getImageList(path):
    for (dirpath, dirnames, filenames) in walk(path):
        return filenames

# Demo printing file names
filelist = getImageList("path/to/image/dir") 
for fileName in fileList:
    print(fileName)

getImageList(path) function returns all files (not directories) in a given path. Place all your images inside a directory, and use the function to get the file list.

Hope this helped.



来源:https://stackoverflow.com/questions/46739945/reading-images-in-python-using-opencv-and-os-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!