How to iterate through images in a folder Python? Last Updated : 21 Jul, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to iterate through images in a folder in Python. Method 1: Using os.listdirExample 1: Iterating through .png onlyAt first we imported the os module to interact with the operating system.Then we import listdir() function from os to get access to the folders given in quotes.Then with the help of os.listdir() function, we iterate through the images and printed the names in order.Here we have mentioned only .png files to be loaded using the endswith() function. Python3 # import the modules import os from os import listdir # get the path/directory folder_dir = "C:/Users/RIJUSHREE/Desktop/Gfg images" for images in os.listdir(folder_dir): # check if the image ends with png if (images.endswith(".png")): print(images) Output: Example 2: Iterating through all kinds of images Here we have mentioned .png, .jpg, .jpeg files to be loaded using the endswith() function. Python3 # import the modules import os from os import listdir # get the path or directory folder_dir = "C:/Users/RIJUSHREE/Desktop/Gfg images" for images in os.listdir(folder_dir): # check if the image ends with png or jpg or jpeg if (images.endswith(".png") or images.endswith(".jpg")\ or images.endswith(".jpeg")): # display print(images) Output: Method 2: Using pathlib moduleAt first, we imported the pathlib module from Path.Then we pass the directory/folder inside Path() function and used it .glob('*.png') function to iterate through all the images present in this folder. Python3 # import required module from pathlib import Path # get the path/directory folder_dir = 'Gfg images' # iterate over files in # that directory images = Path(folder_dir).glob('*.png') for image in images: print(image) Output: Method 3: Using glob.iglob()At first we imported the glob module.Then with the help of glob.iglob() function we iterate through the images and print the names in order.Here we have mentioned .png files to be loaded using the endswith() function. Python3 # import required module import glob # get the path/directory folder_dir = 'Gfg images' # iterate over files in # that directory for images in glob.iglob(f'{folder_dir}/*'): # check if the image ends with png if (images.endswith(".png")): print(images) Output: Comment More infoAdvertise with us Next Article How to iterate through images in a folder Python? rijushree100guha Follow Improve Article Tags : Misc Python Python directory-program Practice Tags : Miscpython Similar Reads Show Random Picture from a Folder in Python In this article, we are going to share with you the steps to create a simple script that selects and displays a random image from a specified folder using Python. By using the combination of os, random, and PIL libraries, you can easily do this, So, let's get started. Showing Random Picture from a F 3 min read How to Download All Images from a Web Page in Python? Prerequisite: Requests BeautifulSouposFile Handling Web scraping is a technique to fetch data from websites. While surfing on the web, many websites donât allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious and time-consuming. Web Scraping is 3 min read How to extract images from PDF in Python? The task in this article is to extract images from PDFs and convert them to Image to PDF and PDF to Image in Python.To extract the images from PDF files and save them, we use the PyMuPDF library. First, we would have to install the PyMuPDF library using Pillow.pip install PyMuPDF PillowPyMuPDF is us 3 min read How to download an image from a URL in Python Downloading content from its URL is a common task that Web Scrapers or online trackers perform. These URLs or Uniform Resource Locators can contain the web address (or local address) of a webpage, website, image, text document, container files, and many other online resources. It is quite easy to do 3 min read Delete all the Png Images from a Folder in Python Python is mostly used to automate tasks, including file management operations. Deleting all PNG images from a folder can be efficiently handled using Python. In this article, we will explore two different approaches to Deleting all the PNG images from a Folder in Python. Delete all the PNG images fr 2 min read How to iterate over files in directory using Python? Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.Using 3 min read Rename a folder of images using Tkinter Prerequisites: Python GUI â tkinter, os.listdir() method, os.rename() method In this article, the task is to rename a batch of images using Python. Now say given n images in a folder having random names. For example, consider the images below. Now the requirement is to rename them in ordered fashion 3 min read How to download Google Images using Python Python is a multi-purpose language and widely used for scripting. We can write Python scripts to automate day-to-day things. Letâs say we want to download google images with multiple search queries. Instead of doing it manually we can automate the process. How to install needed Module : pip install 2 min read How to open an image from the URL in PIL? In this article, we will learn How to open an image from the URL using the PIL module in python. For the opening of the image from a URL in Python, we need two Packages urllib and Pillow(PIL). Approach:Install the required libraries and then import them. To install use the following commands:pip ins 1 min read Like