Do you want to get the list of Image names present in the Folder using Python? This article will show you how to read Image Name from Folder in Python.
If you have various files including image files in a folder and you want to extract only the files which are images then in that case you need to know the extension of image files. Generally, you can verify the extension names of those image files to be either JPG, JPEG, PNG, Or HEIC directly using the explorer.
Once you have all the extension names of the image files we need to loop through all the files in a folder using the os library present in Python.
This method can also be used to Iterate through images in a Folder using Python.
Using os.listdir Method to read Image Name from a Folder
You can use the os.listdir method to read the image name from the folder and then print those image paths with filenames or just store the name of the images present in that folder.
Let us see in the below example code the usage of os.listdir() method. For the purpose of this example, I have created a new folder and added 8 Files with file names starting as Test File 1, Test File 2, etc. There is a total of 8 files in my folder out of which 6 are images.
#importing OS to scan through Folders import os #Initializing the Directory to iterate for Images folderPath = "C:\Test\\" #Creating the Tupple of Probable Extensions of image Files extensionList = ('jpg', 'heic', 'png', 'jpeg') #Initializing the List to save the Image Names ImageNames =  #Interating throug each Files in The Folder to #Read the Image Name for currentFileName in os.listdir(folderPath): if currentFileName.endswith(extensionList): print(currentFileName) else: continue
Test File 1.jpg Test File 2.jpg Test File 3.jpg Test File 4.jpg Test File 6.jpeg Test File 8.heic
As you can see in the above output, I was able to read only the image names from the folder using the listdir method of the OS library in Python.
I have created a Tuple to check if the file ends with the list of strings present in the Tuple. If you have larger extension names you can update the Tuple to your wish.
Using glob Method To Iterate Over Image in Folder
You can use the glob library as this module can be used to get all the images in the folder those are matching the specified extension or pattern. Here the list can be printed or listed in any order.
Let us see in the below example code the usage of the glob module. For the purpose of this example, I have created a new folder and added 8 Files with file names starting as Test File 1, Test File 2, etc. There is a total of 8 files in my folder out of which 6 are images.
#importing glob to scan through Folders import glob #Initializing the Directory to iterate for Images folderPath = "C:\Test\\" #Creating the Tupple of Probable Extensions of image Files extensionList = ('*.jpg', '*.heic', '*.png', '*.jpeg') #Initializing the List to save the Image Names ImageNames =  #Interating throug each Files in The Folder to #Read the Image Names based on Extension. for extension in extensionList: for currentFilePath in glob.iglob(folderPath+"\\"+extension): print(currentFilePath) ImageNames.append(currentFilePath)
C:\Test\Test File 1.jpg C:\Test\Test File 2.jpg C:\Test\Test File 3.jpg C:\Test\Test File 4.jpg C:\Test\Test File 8.heic C:\Test\Test File 6.jpeg
I hope you were able to get the answer related to either how to iterate over images in a folder using Python or How to Read Images Names in a Folder in Python. You can use any one of the methods that suit your requirement.
If you have any better method than the one mentioned above you can write that in the comment section I will be happy to add it here.
- How To Return Null in Python Discussed
- How to Split a String into a List of Words or Letters in Python
- Reading A Binary File In Python With Examples
- How To Check If The Variable is An Integer in Python
- Python – Check If String is an Integer or Float
- Python – Convert Bytes To String Quickly