问题
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