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 Folder in Python
Using the Python Pillow library, we can show the picture randomly from any folder. Here is the step-by-step procedure for doing this.
Step 1: Install the Required Libary
pip install pillow
Step 2: Getting the image path randomly from the folder
Python
#import necessary package importosimportrandomfromPILimportImagedefget_random_image_path(folder_path):try:files=os.listdir(folder_path)# Filter the list to get only image files images=[fileforfileinfilesiffile.lower().endswith(('.png','.jpg','.jpeg','.gif'))]ifnotimages:print("No images found in the specified folder.")returnNone# Choose a random image filerandom_image=random.choice(images)random_image_path=os.path.join(folder_path,random_image)print(f"Here the random image path from the folder:")returnrandom_image_pathexceptExceptionase:print(f"An error occurred while selecting image randomly: {e}")returnNone
Example Output:
Suppose, the images are in this folder, "/home/somraj/All Data/somraj image folder" Here the random image path from the folder: /home/somraj/All Data/somraj image folder/image1.png
Step 3: Displaying the random Image using PIL
Python
#imporing all necessary packagesimportosfromPILimportImagedefdisplay_image(image_path):try:ifimage_pathandos.path.isfile(image_path):withImage.open(image_path)asimg:#opening the random image img.show()print(f"Displayed image: {image_path}")else:print(f"Invalid image path: {image_path}")exceptExceptionase:print(f"An error occurred while displaying the image: {e}")
Example Output: Display image: /home/somraj/All Data/somraj image folder/image1.png
Step 4: Integrating both function to showing the random Image
Python
defshow_random_image_from_folder(folder_path):random_image_path=get_random_image_path(folder_path)display_image(random_image_path)# usage:if__name__=="__main__":folder_path='/path/to/your/image_folder'ifos.path.isdir(folder_path):show_random_image_from_folder(folder_path)else:print(f" The specified folder does not exist: {folder_path}")
Full code
Python
importosimportrandomfromPILimportImagedefget_random_image_path(folder_path):try:files=os.listdir(folder_path)# Filter the list to get only image filesimages=[fileforfileinfilesiffile.lower().endswith(('.png','.jpg','.jpeg','.gif'))]ifnotimages:print("No images found in the specified folder.")returnNone# Choose a random image filerandom_image=random.choice(images)random_image_path=os.path.join(folder_path,random_image)returnrandom_image_pathexceptExceptionase:print(f"An error occurred while selecting image randomly: {e}")returnNonedefdisplay_image(image_path):try:ifimage_pathandos.path.isfile(image_path):withImage.open(image_path)asimg:img.show()print(f"Displayed image: {image_path}")else:print(f"Invalid image path: {image_path}")exceptExceptionase:print(f"An error occurred while displaying the image: {e}")defshow_random_image_from_folder(folder_path):random_image_path=get_random_image_path(folder_path)display_image(random_image_path)# usage:if__name__=="__main__":folder_path='/path/to/your/image_folder'ifos.path.isdir(folder_path):show_random_image_from_folder(folder_path)else:print(f" The specified folder does not exist: {folder_path}")
Example Output:
Here the random image path from the folder: /home/somraj/All Data/somraj image folder/image1.png Display image: /home/somraj/All Data/somraj image folder/image1.png
NOTE: The folder structure of showing a Random Image from a Folder will be like this,